summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-10-16 22:40:09 +0600
committerunwox <me@unwox.com>2024-10-16 22:43:17 +0600
commitfd9a559475ac6bff7c82594dc313d762ab807592 (patch)
tree5ae19da434713cb31fe9747a2f713eb3506936f9 /bin
parentc760c798a56fb80c891f26ca932e7d182da67069 (diff)
improve site structure for seo
Diffstat (limited to 'bin')
-rw-r--r--bin/serve.fnl134
1 files changed, 93 insertions, 41 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl
index 4cbb905..62e3962 100644
--- a/bin/serve.fnl
+++ b/bin/serve.fnl
@@ -11,7 +11,7 @@
(local str (require :lib.string))
(local texts (require :texts))
(local spellfix (require :spellfix))
-(local {: must} (require :lib.utils))
+(local {: must : measure} (require :lib.utils))
(local ozchai (require :parser.ozchai))
(local ipuer (require :parser.ipuer))
@@ -39,6 +39,16 @@
(string.gsub "&quot;" "\"")
(string.gsub "&amp;" "&"))))
+(fn category-menu-path [category]
+ (. {"Красный чай" "red-tea"
+ "Шен пуэр" "sheng-puer"
+ "Шу пуэр" "shou-puer"
+ "Улун" "oolong"
+ "Зеленый чай" "green-tea"
+ "Белый чай" "white-tea"
+ "Желтый чай" "yellow-tea"
+ "Посуда" "teaware"} category))
+
(fn get-query-string [query key]
(if (and query
(. query key)
@@ -55,9 +65,9 @@
(tonumber (. query key 1))
nil))
-(fn collect-form [params]
+(fn collect-form [params category]
{:query (str.trim (or (get-query-string params "query") ""))
- :tags (filter #(~= "" $2) (or params.tags []))
+ :tags (filter #(~= "" $2) (or params.tags [category]))
:min-price (get-query-number params "min-price")
:max-price (get-query-number params "max-price")
:price-per (= "on" (get-query-string params "price-per"))})
@@ -140,7 +150,7 @@
(if (. query-synonyms q)
(.. "(" q "* OR " (. query-synonyms q) "*)")
(.. q "*")))
- (str.split query))
+ (str.split query " "))
" AND ")))
(when (and min-price (< 0 min-price))
(if price-per
@@ -202,29 +212,24 @@
(fn site-name-template [name]
(if
(= name "ipuer")
- [:a {:class "site-icon" :href "https://ipuer.ru"
- :alt "Логотип Институт чай пуэр"}
- [:img {:src "/static/ipuer.jpg"}]
+ [:a {:class "site-icon" :href "https://ipuer.ru"}
+ [:img {:src "/static/ipuer.jpg" :alt "Логотип Институт чай пуэр"}]
"Институт чая пуэр"]
(= name "artoftea")
- [:a {:class "site-icon" :href "https://artoftea.ru"
- :alt "Логотип Art of tea"}
- [:img {:src "/static/artoftea.png"}]
+ [:a {:class "site-icon" :href "https://artoftea.ru"}
+ [:img {:src "/static/artoftea.png" :alt "Логотип Art of tea"}]
"Art of tea"]
(= name "ozchai")
- [:a {:class "site-icon" :href "https://ozchai.ru"
- :alt "Логотип #OZCHAI"}
- [:img {:src "/static/ozchai.ico"}]
+ [:a {:class "site-icon" :href "https://ozchai.ru"}
+ [:img {:src "/static/ozchai.ico" :alt "Логотип #OZCHAI"}]
"Чайная #OZCHAI"]
(= name "clubcha")
- [:a {:class "site-icon" :href "https://clubcha.ru"
- :alt "Логотип Железный Феникс"}
- [:img {:src "/static/clubcha.png"}]
+ [:a {:class "site-icon" :href "https://clubcha.ru"}
+ [:img {:src "/static/clubcha.png" :alt "Логотип Железный Феникс"}]
"Железный Феникс"]
(= name "chaekshop")
- [:a {:class "site-icon" :href "https://chaekshop.ru"
- :alt "Логотип Интернет-магазин фермерского китайского чая «Чаёк»"}
- [:img {:src "/static/chaekshop.svg"}]
+ [:a {:class "site-icon" :href "https://chaekshop.ru"}
+ [:img {:src "/static/chaekshop.svg" :alt "Логотип Интернет-магазин «Чаёк»"}]
"Интернет-магазин «Чаёк»"]
""))
@@ -267,7 +272,7 @@
(if (< page last-page)
[:a {:href (form->path (+ page 1) form)} "→"]
"")]
- [:div {} "Всего результатов: " [:strong {} (string.format "%d" total)]]]
+ [:div {} "Всего: " [:strong {} (string.format "%d" total)]]]
""))
(fn aside-template [form paginator]
@@ -275,9 +280,9 @@
[:div {:class "aside-content"}
(if (not (form-empty? form))
[:a {:href "/" :style "display: block;"}
- [:img {:class "logo" :src "static/logo.svg"
+ [:img {:class "logo" :src "/static/logo.svg"
:alt "Логотип meicha.ru" :title "Логотип meicha.ru"}]]
- [:img {:class "logo" :src "static/logo.svg"
+ [:img {:class "logo" :src "/static/logo.svg"
:alt "Логотип meicha.ru" :title "Логотип meicha.ru"}])
[:form {:class "form"}
[:input {:type :search :name "query" :value form.query
@@ -305,22 +310,49 @@
[:button {:type :submit} "Искать"]]
paginator]])
+(fn menu-template []
+ [:nav {:class "menu"}
+ [:a {:href "/red-tea"} "Красный чай"]
+ [:a {:href "/sheng-puer"} "Шен пуэр"]
+ [:a {:href "/shou-puer"} "Шу пуэр"]
+ [:a {:href "/oolong"} "Улун"]
+ [:a {:href "/green-tea"} "Зеленый чай"]
+ [:a {:href "/white-tea"} "Белый чай"]
+ [:a {:href "/yellow-tea"} "Желтый чай"]
+ [:a {:href "/teaware"} "Посуда"]])
+
(fn base-template [form page total items]
(local paginator (paginator-template form page 48 total))
+ (local menu-path
+ (if (and form.tags (< 0 (# form.tags)))
+ (category-menu-path (. form.tags 1))
+ nil))
(local spellfix-suggestion
(if (and (not (str.empty? form.query)) items (< 0 (# items)))
nil
(spellfix.guess form.query)))
- [:html {:lang "en"}
+ [:html {:lang "ru-RU"}
[:head {}
- [:meta {:charset "UTF-8"}]
- [:link {:rel :stylesheet :href "static/style.css"}]
- [:title {} "A new cool web server for lua"]]
+ [:meta {:charset "utf-8"}]
+ [:meta {:name "viewport" :content "width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=5.0"}]
+ [:link {:rel "stylesheet" :href "/static/style.css"}]
+ [:link {:rel "icon" :href "/static/favicon.png"}]
+ [:title {} (if (str.empty? form.query)
+ texts.meta-title
+ (.. form.query " | " texts.meta-title))]
+ [:meta {:name "description" :content texts.meta-description}]]
[:body {}
[:div {:class "container"}
[:div {:class "content"}
(aside-template form paginator)
[:section {}
+ (menu-template)
+ (if (. form.tags 1)
+ [:h1 {} (. form.tags 1)]
+ "")
+ (if (and menu-path (. texts (.. menu-path "-description")))
+ [:NO-ESCAPE (. texts (.. menu-path "-description"))]
+ "")
(if (< 0 (# items))
[:div {:class "list"}
(table.unpack (map #(item-template $2) items))]
@@ -333,20 +365,40 @@
[:footer {} paginator]]]]]])
(fn root-handler [{: path : query}]
- (if (= path "/")
- (let [headers {:content-type "text/html"}
- page (or (get-query-number query "page") 1)
- form (collect-form query)
- {: results : total}
- (if (not (form-empty? form))
- (query-products form page)
- {:total 48 :results (random-products 48)})]
- (values
- 200 headers
- (html.render
- (base-template form page total results)
- true)))
- (values 404 {} "not found")))
+ (fn respond [query category]
+ (let [headers {:content-type "text/html"}
+ page (or (get-query-number query "page") 1)
+ form (collect-form query category)
+ {: results : total}
+ (if (not (form-empty? form))
+ (measure "query products" #(query-products form page))
+ {:total 48 :results (measure "random products" #(random-products 48))})]
+ (values
+ 200 headers
+ (.. "<!DOCTYPE html>\n"
+ (measure "render"
+ #(html.render
+ (base-template form page total results)
+ true))))))
+
+ (match (str.split path "/")
+ ["red-tea"] (respond query "Красный чай")
+ ["sheng-puer"] (respond query "Шен пуэр")
+ ["shou-puer"] (respond query "Шу пуэр")
+ ["oolong"] (respond query "Улун")
+ ["green-tea"] (respond query "Зеленый чай")
+ ["white-tea"] (respond query "Белый чай")
+ ["yellow-tea"] (respond query "Желтый чай")
+ ["teaware"] (respond query "Посуда")
+ [] (respond query)
+ _ (values 404 {} "not found")))
+
+(fn robots-handler []
+ (values 200 {:content-type "text/plain"} "User-agent: *\nAllow: /"))
-(must (luna.router.route "GET /" root-handler))
+(must (luna.router.route
+ "GET /"
+ (fn [req]
+ (measure "root handler" #(root-handler req)))))
+(must (luna.router.route "GET /robots.txt" robots-handler))
(must (luna.router.static "GET /static/" "static/"))