diff options
| author | unwox <me@unwox.com> | 2025-10-03 11:56:37 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2025-10-13 23:11:01 +0600 |
| commit | 3f5ade2e7a139bb4405437e8fc5546aafc7b05ef (patch) | |
| tree | 77c437958d74b591f11ec207d16749cf207a51e3 /bin/serve.fnl | |
| parent | f5a70e6a446e00969adb866ef2e2d10bf33bc4a8 (diff) | |
WIP shop
Diffstat (limited to 'bin/serve.fnl')
| -rw-r--r-- | bin/serve.fnl | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl index 359bfd9..0948a83 100644 --- a/bin/serve.fnl +++ b/bin/serve.fnl @@ -1,17 +1,12 @@ (local lib (require :lib)) -(when _G.unpack +( when _G.unpack (tset table :unpack _G.unpack)) (fn _G.must [...] (local (ok? result) ...) (if ok? result (error result))) -(fn _G.pp [...] - (local args (table.pack ...)) - (for [i 1 args.n] - (print (fennel.view (. args i))))) - (fn _G.reload [module] (local old (require module)) (tset package :loaded module nil) @@ -90,8 +85,10 @@ id TEXT PRIMARY KEY, creation_time TEXT NOT NULL, placement_time TEXT, - first_name TEXT, - contact TEXT + state TEXT NOT NULL DEFAULT 'cart', + name TEXT, + contact TEXT, + consent BOOLEAN ); CREATE TABLE IF NOT EXISTS order_lines( @@ -174,9 +171,24 @@ (test "shop/xyz/edit" "shop/_product/edit" {:_product "xyz"}))) (test-match-route) - (local routes (scan-routes "pages")) +(fn authenticate-request [db request] + (let [cookies-header (. request.headers :Cookie) + cookies (if cookies-header (lib.parse-values cookies-header) {}) + session-id cookies.auth] + (if (not (lib.empty? session-id)) + (let [sessions + (_G.must + (luna.db.query-assoc + db + "SELECT id FROM auth_sessions + WHERE id = ? + AND expires_at > STRFTIME('%Y-%m-%d %H:%M:%S', DATETIME('now'))" + [session-id]))] + (< 0 (# sessions))) + false))) + (fn router [request] (if (and (lib.ends-with? request.path "/") (~= request.path "/")) @@ -185,16 +197,13 @@ (tset request :params params) (if (and (= (type handler) "table") handler.render) (let [(code headers content) - (handler.render request db (lib.authenticate-request db request))] + (handler.render request db (authenticate-request db request))] (values code headers (.. "<!DOCTYPE html>\n" content))) (values 404 {:content-type "text/html"} "not found"))))) -(_G.must - (luna.router.route "/" router)) +(_G.must (luna.router.route "/" router)) (_G.must (luna.router.static "GET /static" "static/" "")) -(_G.must - (luna.router.static "GET /static/files" - "static/files" "")) +(_G.must (luna.router.static "GET /static/files" "static/files" "")) (when (= 0 (# (_G.must (luna.db.query db "SELECT name FROM users LIMIT 1" [])))) (let [password (_G.must (luna.crypto.random-string 20)) |
