1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
(import-macros {: map : reduce : filter} :lib.macro)
(tset package :path (.. package.path ";./vendor/lpeglj/?.lua"))
(local array (require :lib.array))
(local cache (require :lib.cache))
(local fennel (require :vendor.fennel))
(local fs (require :lib.fs))
(local html (require :vendor.html))
(local libhtml (require :lib.html))
(local math (require :math))
(local spellfix (require :lib.spellfix))
(local libstr (require :lib.string))
(local synonyms (require :lib.synonyms))
(local texts (require :texts))
(local {: must} (require :lib.utils))
(when _G.unpack
(tset table :unpack _G.unpack))
(set _G.reload
(fn [module]
(local old (require module))
(tset package :loaded module nil)
(local (ok? new) (pcall require module))
(if (not ok?)
(do
(tset package :loaded module old)
(error new))
(when (= (type new) :table)
(do
(each [k v (pairs new)]
(tset old k v))
(each [k (pairs old)]
(when (not (. new k))
(tset old k nil)))
(tset package :loaded module old))))))
(local db
(must (luna.db.open "file:var/db.sqlite?_journal=WAL&_sync=NORMAL&_txlock=immediate")))
(fn sanitize-input [input]
(if input
(libstr.trim (input:gsub "[=()<>']" "") " ")
nil))
(fn query-string [query key]
(if (and query
(. query key)
(. query key 1)
(< 0 (# (. query key 1))))
(sanitize-input (. query key 1))
nil))
(fn query-number [query key]
(if (and query
(. query key)
(. query key 1)
(< 0 (# (. query key 1))))
(tonumber (sanitize-input (. query key 1)))
nil))
(fn serialize-query [query]
(local flattened-object
(collect [k v (pairs query)]
(values k (array.join (map (fn [_ vv] (.. k "=" vv)) v) "&"))))
(array.join (array.list flattened-object) "&"))
(fn collect-form [params]
{:query (or (query-string params "query") "")
:page (or (query-number params "page") 1)
:tags (map #(sanitize-input $2)
(filter #(~= "" $2) (or params.tags [])))
:site (or (query-string params "site") "")
:sort (or (query-string params "sort") "")
:min-price (query-number params "min-price")
:max-price (query-number params "max-price")
:min-volume (query-number params "min-volume")
:max-volume (query-number params "max-volume")
:price-per (= "on" (query-string params "price-per"))})
(fn form-empty? [form]
(and
(= "" form.query)
(= (# form.tags) 0)
(= "" form.site)
(not form.min-price)
(not form.max-price)
(not form.min-volume)
(not form.max-volume)
;; price-per and sort are intentionally left out since they must not
;; trigger search by itself
))
(fn form->path [page form]
(.. "?page=" (tostring page)
(if (not (libstr.empty? form.query))
(.. "&query=" form.query)
"")
(if (< 0 (# form.tags))
(.. "&"
(array.join
(array.flatten
(map (fn [_ tag] (.. "tags=" tag))
form.tags))
"&"))
"")
(if (not (libstr.empty? form.site))
(.. "&site=" form.site)
"")
(if (and form.min-price (< 0 form.min-price))
(.. "&min-price=" (tostring form.min-price))
"")
(if (and form.max-price (< 0 form.max-price))
(.. "&max-price=" (tostring form.max-price))
"")
(if form.price-per
"&price-per=on"
"")
(if (and form.min-volume (< 0 form.min-volume))
(.. "&min-volume=" (tostring form.min-volume))
"")
(if (and form.max-volume (< 0 form.max-volume))
(.. "&max-volume=" (tostring form.max-volume))
"")
(if form.sort
(.. "&sort=" form.sort)
"")))
(fn teas-of-the-day [limit]
(assert (< 0 limit) "limit must be > 0")
(local the-number (* (tonumber (os.date "%Y%m%d")) limit))
(local total
(. (must (luna.db.query db
"SELECT count(ROWID)
FROM (
SELECT products.ROWID
FROM products
INNER JOIN product_tags ON product_tags.product = products.url
WHERE archived = false
AND product_tags.tag IN ('Красный чай', 'Улун', 'Шен пуэр',
'Шу пуэр', 'Зеленый чай', 'Белый чай',
'Желтый чай')
GROUP BY products.url)" []))
1 1))
(must
(luna.db.query-assoc
db
"SELECT products.site,
products.title,
products.description,
products.image,
products.url,
products.price,
products.weight,
products.price_per AS \"price-per\"
FROM products
INNER JOIN product_tags ON product_tags.product = products.url
WHERE products.archived = false
AND product_tags.tag IN ('Красный чай', 'Улун', 'Шен пуэр', 'Шу пуэр',
'Зеленый чай', 'Белый чай', 'Желтый чай')
GROUP BY products.url
ORDER BY ROW_NUMBER() over (PARTITION BY site ORDER BY products.ROWID)
LIMIT ?
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))
(must
(luna.db.query
db
"SELECT DISTINCT tags.title FROM tags
INNER JOIN product_tags ON product_tags.tag = tags.title
ORDER BY creation_time" []))))
(fn shops-count []
(# (must (luna.db.query db
"SELECT DISTINCT site
FROM products
WHERE archived = false" []))))
(fn products-count []
(. (must (luna.db.query db
"SELECT DISTINCT COUNT(url)
FROM products
WHERE archived = false" []))
1 1))
(fn latest-update-date []
(. (must (luna.db.query db
"SELECT update_time
FROM products
ORDER BY update_time DESC
LIMIT 1" []))
1 1))
(fn query-products [{: query : tags : min-price : max-price : price-per
: min-volume : max-volume : site : sort} page]
(local tags (or tags []))
(local where-conds [])
(local sort-criteria [])
(local query-args [])
;; create WHERE clause from dynamic form parameters
(when (< 0 (# tags))
(each [_ tag (pairs tags)]
(table.insert where-conds "product_tags.tag = ?")
(table.insert query-args tag)))
(when (and query (< 0 (# query)))
(table.insert where-conds "search.title MATCH ?")
(table.insert query-args (synonyms.replace query)))
(when (not (libstr.empty? site))
(table.insert where-conds "products.site = ?")
(table.insert query-args site))
(when (and min-price (< 0 min-price))
(if price-per
(table.insert where-conds "products.price_per >= ?")
(table.insert where-conds "products.price >= ?"))
(table.insert query-args min-price))
(when (and max-price (< 0 max-price))
(if price-per
(table.insert where-conds "products.price_per <= ?")
(table.insert where-conds "products.price <= ?"))
(table.insert query-args max-price))
(when (and min-volume (< 0 min-volume))
(table.insert where-conds "products.volume >= ?")
(table.insert query-args min-volume))
(when (and max-volume (< 0 max-volume))
(table.insert where-conds
"(products.volume <= ? AND products.volume != 0)")
(table.insert query-args max-volume))
(local where-sql
(if (< 0 (# where-conds))
(.. "AND " (array.join where-conds "\nAND "))
""))
;; create SORT clause from dynamic form parameters
(when sort
(if (= sort "cheap-first")
(table.insert sort-criteria
;; multiple price by 100 if we only know a price for 1 gram
"CASE WHEN products.weight = 1 THEN products.price * 100
ELSE products.price
END ASC")
(= sort "expensive-first")
(table.insert sort-criteria
;; multiple price by 100 if we only know a price for 1 gram
"CASE WHEN products.weight = 1 THEN products.price * 100
ELSE products.price
END DESC")))
(local sort-sql
(if (< 0 (# sort-criteria))
(.. (array.join sort-criteria ",\n") ",")
""))
(local total
(must
(luna.db.query
db
(string.format
"SELECT count(ROWID)
FROM (
SELECT products.ROWID
FROM search
INNER JOIN products ON search.fid = products.url
LEFT JOIN product_tags ON product_tags.product = search.fid
WHERE search.`table` = 'products'
AND products.archived = false
%s
GROUP BY products.url)" where-sql)
query-args)))
{:results
(must
(luna.db.query-assoc
db
(string.format
"SELECT products.title,
products.site,
products.description,
products.image,
products.url,
products.price,
products.weight,
products.price_per AS \"price-per\"
FROM search
INNER JOIN products ON search.fid = products.url
LEFT JOIN product_tags ON product_tags.product = products.url
LEFT JOIN permanent_products ON permanent_products.url = products.url
WHERE search.`table` = 'products'
AND products.archived = false
%s
GROUP BY products.url
ORDER BY %s
ROW_NUMBER() OVER
(PARTITION BY products.site
ORDER BY permanent_products.creation_time DESC,
products.title ASC),
rank
LIMIT 24 OFFSET ?" where-sql sort-sql)
(array.concat query-args [(* (- page 1) 24)])))
:total (if (< 0 (# total))
(. total 1 1)
0)})
(fn site-name-template [name]
(local module (require (.. "parser." name)))
[:a {:class "site-icon"
:href (tracked-url (.. module.url "?from=everytea.ru"))}
[:img {:src (.. "/static/" name ".webp") :alt (.. "Логотип " module.title)}]
module.title])
(fn item-template [product]
(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
:alt product.title} ""]]
(site-name-template product.site)
[:a {:href link :style "text-decoration: none;" :rel "nofollow"}
[:NO-ESCAPE (.. "<div class=\"title\">"
(libhtml.unescape product.title)
"</div>")]]
[:div {:class "price"}
(if product.price (.. (libstr.format-price product.price) "₽") "")
(if (< 0 product.weight)
[:NO-ESCAPE (.. "за " product.weight " гр.")]
"")
(if (and (< 1 product.weight)
product.price-per
(< 0 product.price-per))
[:NO-ESCAPE (.. "<br> (" (libstr.format-price product.price-per)
"₽ за 1 гр.)")]
"")]])
(fn paginator-template [form page limit total max-numbers]
(local last-page (math.ceil (/ total limit)))
(local radius (math.floor (/ (- max-numbers 4) 2)))
(local number-items
(if (<= last-page max-numbers)
(faccumulate [res [:span {}] i 1 last-page]
(do
(table.insert
res [:a {:href (form->path i form)
:class (if (= page i) "paginator-active" "")}
(tostring i)])
res))
[:span {}
(table.unpack
(array.concat
(if (< (+ 1 radius) page)
[[:a {:href (form->path 1 form)} "1"]
(if (< (+ 2 radius) page) [:span {} "…"] "")]
[])
(faccumulate [res [] i (math.max 1 (- page radius))
(math.min last-page (+ page radius))]
(do
(table.insert
res [:a {:href (form->path i form)
:class (if (= page i) "paginator-active" "")}
(tostring i)])
res))
(if (< radius (- last-page page))
[(if (< (+ 1 radius) (- last-page page)) [:span {} "…"] "")
[:a {:href (form->path last-page form)} (tostring last-page)]]
[])))]))
(if (< limit total)
[:div {:class "paginator"}
[:div {:class "paginator-numbers"} number-items]
[:div {} "Всего: " [:strong {} (string.format "%d" total)]]]
""))
(fn menu-template [path extra-class]
(local item
(fn [item-path title]
[:a {:href item-path
:class (if (= path item-path) "active" "")}
title]))
[:nav {:class (.. "menu" (if (libstr.empty? extra-class)
""
(.. " " extra-class)))}
(item "/red-tea" "Красный чай")
(item "/sheng-puer" "Шен пуэр")
(item "/shou-puer" "Шу пуэр")
(item "/oolong" "Улун")
(item "/green-tea" "Зеленый чай")
(item "/white-tea" "Белый чай")
(item "/yellow-tea" "Желтый чай")
(item "/teaware" "Посуда")])
(fn aside-template [form path aside-content]
(local teaware? (array.contains form.tags "Посуда"))
[:aside {:class "aside"}
[:div {:class "aside-content"}
(if (or (not (form-empty? form)) (~= path "/"))
[:a {:href "/" :style "display: block;"}
[:img {:class "logo" :src "/static/logo.svg"
:alt "Логотип everytea.ru" :title "Логотип everytea.ru"}]]
[:img {:class "logo" :src "/static/logo.svg"
:alt "Логотип everytea.ru" :title "Логотип everytea.ru"}])
(menu-template path "menu-mobile")
[:form {:class "form"}
[:input {:type :search :name "query" :value form.query
;; autofocus makes browser scroll up to the input on reloads
;; and that doesn't work well with _G.reload
:autofocus (if luna.debug nil true)
:placeholder "Поисковый запрос"}]
[:div {}
[:select {:name "tags"}
[:option {:value ""} "~ Тег ~"]
(table.unpack
(map
(fn [_ tag]
[:option {:value tag
:selected (if (array.contains form.tags tag)
"selected" nil)}
tag])
(all-tags)))]]
[:div {:class "form-range"}
[:input {:type :number :name "min-price" :min "1"
:placeholder "От ₽" :value (tostring form.min-price)}]
[:input {:type :number :name "max-price" :min "1"
:placeholder "До ₽" :value (tostring form.max-price)}]]
(if teaware?
[:div {:class "form-range"}
[:input {:type :number :name "min-volume" :min "1"
:placeholder "От мл" :value (tostring form.min-volume)}]
[:input {:type :number :name "max-volume" :min "1"
:placeholder "До мл" :value (tostring form.max-volume)}]]
"")
(if (not teaware?)
[:div {:class "form-price-per"}
[:input {:type :checkbox :id "price-per" :name "price-per"
:checked (if form.price-per "checked" nil)}]
[:label {:for "price-per"} "цена за грамм"]]
"")
[:div {}
[:select {:name "site"}
[:option {:value ""} "~ Сайт ~"]
(table.unpack
(map
(fn [_ val]
[:option {:value val
:selected (if (= form.site val) "selected" nil)}
(. (require (.. "parser." val)) :title)])
[:suhexuan :ozchai :kolokolnikovchai :yoceramics :tea108
:ipuer :clubcha :daochai :chaekshop :teaworkshop :artoftea
:moychay :gorkovchay]))]]
[:div {}
[:select {:name "sort"}
[:option {:value ""} "~ Порядок ~"]
[:option {:value "cheap-first"
:selected (if (= form.sort "cheap-first") "selected" nil)}
"Сначала дешевле"]
[:option {:value "expensive-first"
:selected (if (= form.sort "expensive-first") "selected" nil)}
"Сначала дороже"]]]
[:button {:type :submit} "Искать"]]
(if aside-content aside-content "")]])
(fn home-template []
[:div {}
[:article {:class "description"}
[:h1 {} "Добро пожаловать!"]
[:div {:class "description-text text"}
[:p {} [:NO-ESCAPE
(string.format texts.home-text
(shops-count)
(products-count)
(latest-update-date))]]]]
(comment [:section {}
[:h2 {} "Чаи дня"]
[:div {:class "list"}
(table.unpack (map #(item-template $2) (teas-of-the-day 10)))]])])
(fn base-template [form path content aside-content]
(local title (if (libstr.empty? form.query)
texts.meta-title
(.. form.query " | " texts.meta-title)))
(local canonical-url (libstr.trim (.. "https://everytea.ru" path) "/"))
[:html {:lang "ru-RU"}
[:head {}
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content (.. "width=device-width,initial-scale=1,"
"minimum-scale=1.0,maximum-scale=5.0")}]
;; opengraph
[:meta {:property "og:title" :content title}]
[:meta {:property "og:description" :content texts.meta-description}]
[:meta {:property "og:type" :content "website"}]
[:meta {:property "og:url" :content canonical-url}]
[:meta {:property "og:image"
:content "https://everytea.ru/static/og:image.png"}]
[:meta {:property "twitter:image"
:content "https://everytea.ru/static/og:image.png"}]
[:meta {:property "vk:image"
:content "https://everytea.ru/static/og:image.png"}]
[:meta {:property "og:image:width" :content "1200"}]
[:meta {:property "og:image:height" :content "630"}]
[:meta {:property "twitter:card" :content "summary_large_image"}]
[:meta {:property "og:locale" :content "ru_RU"}]
;; inline styles so the page loads faster
[:style {} [:NO-ESCAPE (fs.read-file "static/style.css")]]
[:link {:rel "icon" :href "https://everytea.ru/static/favicon.svg"
:type "image/svg+xml"}]
[:link {:rel "preload" :fetchpriority "high" :as "image"
:href "/static/logo-hor.svg" :type "image/svg+xml"}]
[:link {:rel "canonical" :href canonical-url}]
[:title {} title]
[:meta {:name "description" :content texts.meta-description}]]
[:body {}
[:div {:class "container"}
[:div {:class "content"}
(aside-template form path aside-content)
[:main {}
(menu-template path)
content
[:footer {}
[:small {:class "text"} [:NO-ESCAPE texts.footer-text]]]]]]]])
(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-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"})
(when (and category (= 0 (# form.tags)))
(tset form.tags 1 category))
(local {: results : total} (query-products form form.page))
(local spellfix-suggestion
(if (and (not (libstr.empty? form.query)) results (= 0 (# results)))
(cache.wrap
db (.. "spellfix:" form.query)
#(spellfix.guess form.query))
nil))
(local description-key (.. (libstr.trim path "/") "-description"))
(local page
(base-template form path
[:div {}
(if (and (. form.tags 1) (. texts description-key))
[:article {:class "description"}
[:h1 {} (. form.tags 1)]
[:div {:class "description-text text"}
[:NO-ESCAPE (. texts description-key)]]]
"")
(if (< 0 (# results))
[:div {:class "list"}
(table.unpack (map #(item-template $2) results))]
[:p {:class "text"}
(if spellfix-suggestion
[:NO-ESCAPE
(string.format texts.no-results-with-suggestion
spellfix-suggestion
spellfix-suggestion)]
[:NO-ESCAPE texts.no-results])])
[:div {} (paginator-template form form.page 24 total 17)]]
;; aside
(paginator-template form form.page 24 total 7)))
(values
200 headers
(cache.set db cache-key
(.. "<!DOCTYPE html>\n" (html.render page true)))))
(if (and (not luna.debug) cached)
(values 200 {:content-type "text/html"} cached)
(do
(local form (collect-form query))
(match (libstr.split path "/")
["red-tea"] (render-listing form "Красный чай")
["sheng-puer"] (render-listing form "Шен пуэр")
["shou-puer"] (render-listing form "Шу пуэр")
["oolong"] (render-listing form "Улун")
["green-tea"] (render-listing form "Зеленый чай")
["white-tea"] (render-listing form "Белый чай")
["yellow-tea"] (render-listing form "Желтый чай")
["teaware"] (render-listing form "Посуда")
(where t (= 0 (# t)))
(if (form-empty? form)
(values
200 {:content-type "text/html"}
(.. "<!DOCTYPE html>\n"
(html.render (base-template form path (home-template))
true)))
(render-listing form))
_ (values
404 {:content-type "text/html"}
(.. "<!DOCTYPE html>\n"
(html.render
(base-template form path [:div {} "Страница не найдена!"])
true)))))))
(fn robots-handler []
(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.static "GET /static/" "static/"))
(must (luna.router.route "GET /track" track-handler))
(when luna.debug
(must (luna.on-eval (fn [code] (fennel.eval code {:env _G})))))
|