diff options
| author | unwox <me@unwox.com> | 2025-02-17 17:17:08 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2025-02-17 17:17:08 +0600 |
| commit | c4abe1c6a61826d3a6ecb6053ac76e68e82712c5 (patch) | |
| tree | ea96421ab4b7b3ea80ddff58fe7d39b1614d6101 | |
| parent | 1c48d4edef7ec33d93259a30c2e62dadc2fb29d9 (diff) | |
a little bit of refactoring
| -rw-r--r-- | bin/serve.fnl | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl index de5b4b3..8235468 100644 --- a/bin/serve.fnl +++ b/bin/serve.fnl @@ -28,25 +28,24 @@ (local pattern (or pattern "%s")) (str:match (.. "^" pattern "*(.-)" pattern "*$"))) -(fn file-exists [path] +(fn file-exists? [path] (local f (io.open path "r")) (and (~= f nil) (io.close f))) (fn router [request] - (local path - (trim - (if (ends-with request.path "/") - (.. request.path "index") - request.path) - "/")) - (local module-path (.. "pages." (string.gsub path "%." "/"))) - ;; FIXME: slow - (if (file-exists (.. "pages/" path ".fnl")) - (do - (local (code headers html) - ((. (require module-path) :render) request)) - (values code headers (.. "<!DOCTYPE html>\n" html))) - (values 404 {:content-type "text/html"} "not found"))) + (let + [path (trim + (if (ends-with request.path "/") + (.. request.path "index") + request.path) + "/") + module-path (.. "pages." (string.gsub path "%." "/")) + module-exists? (file-exists? (.. "pages/" path ".fnl"))] + ;; FIXME: slow + (if module-exists? + (let [(code headers html) ((. (require module-path) :render) request)] + (values code headers (.. "<!DOCTYPE html>\n" html))) + (values 404 {:content-type "text/html"} "not found")))) (luna.router.route "GET /" router) (luna.router.static "GET /static/" "static/") |
