From c4abe1c6a61826d3a6ecb6053ac76e68e82712c5 Mon Sep 17 00:00:00 2001 From: unwox Date: Mon, 17 Feb 2025 17:17:08 +0600 Subject: a little bit of refactoring --- bin/serve.fnl | 29 ++++++++++++++--------------- 1 file 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 (.. "\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 (.. "\n" html))) + (values 404 {:content-type "text/html"} "not found")))) (luna.router.route "GET /" router) (luna.router.static "GET /static/" "static/") -- cgit v1.2.3