summaryrefslogtreecommitdiff
path: root/bin/serve.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'bin/serve.fnl')
-rw-r--r--bin/serve.fnl40
1 files changed, 36 insertions, 4 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl
index b498ece..fdd553f 100644
--- a/bin/serve.fnl
+++ b/bin/serve.fnl
@@ -184,6 +184,26 @@
OFFSET ?"
[limit (% the-number (- total limit))])))
+(fn now []
+ (os.date "%Y-%m-%d %H:%M:%S"))
+
+(fn store-metric [headers url]
+ (local fingerprint
+ (must (luna.crypto.sha1
+ (.. (or headers.User-Agent "") ":"
+ (or headers.X-Forwarded-For
+ headers.Forwarded
+ headers.X-Real-Ip
+ "")))))
+ (must
+ (luna.db.exec
+ db
+ "INSERT INTO metrics(fingerprint, url, creation_time) VALUES (?, ?, ?)"
+ [fingerprint url (now)])))
+
+(fn tracked-url [url]
+ (.. "/track?url=" (must (luna.http.encode-url url))))
+
(fn all-tags []
(map
(fn [_ v] (. v 1))
@@ -331,12 +351,13 @@
(fn site-name-template [name]
(local module (require (.. "parser." name)))
- [:a {:class "site-icon" :href (.. module.url "?from=everytea.ru")}
+ [:a {:class "site-icon"
+ :href (tracked-url (.. module.url "?from=everytea.ru"))}
[:img {:src (.. "/static/" name ".png") :alt (.. "Логотип " module.title)}]
module.title])
(fn item-template [product]
- (local link (.. product.url "?from=everytea.ru"))
+ (local link (tracked-url (.. product.url "?from=everytea.ru")))
[:div {:class "tile"}
[:a {:href link :class "img-link" :rel "nofollow"}
[:img {:class "img" :src product.image :title product.title
@@ -545,10 +566,12 @@
[:footer {}
[:small {:class "text"} [:NO-ESCAPE texts.footer-text]]]]]]]])
-(fn root-handler [{: path : query}]
+(fn root-handler [{: headers : path : query}]
+ (local path-with-query (.. path "?" (serialize-query query)))
;; FIXME: do not run render-listing when cache is hit
- (local cache-key (.. "page:" path "?" (serialize-query query)))
+ (local cache-key (.. "page:" path-with-query))
(local cached (cache.get db cache-key))
+ (store-metric headers path-with-query)
(fn render-listing [form category]
(local headers {:content-type "text/html"})
@@ -624,8 +647,17 @@
(values 200 {:content-type "text/plain"}
"User-agent: *\nAllow: /"))
+(fn track-handler [{: headers : query}]
+ (local redirect-url (?. query :url 1))
+ (if redirect-url
+ (do
+ (store-metric headers redirect-url)
+ (values 307 {:Location redirect-url} ""))
+ (values 400 {} "bad redirect url")))
+
(must (luna.router.route "GET /" root-handler))
(must (luna.router.route "GET /robots.txt" robots-handler))
+(must (luna.router.route "GET /track" track-handler))
(must (luna.router.static "GET /static/" "static/"))
(when luna.debug