summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/serve.fnl34
1 files changed, 34 insertions, 0 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl
new file mode 100644
index 0000000..b18d842
--- /dev/null
+++ b/bin/serve.fnl
@@ -0,0 +1,34 @@
+(local fennel (require :vendor.fennel))
+
+(when _G.unpack
+ (tset table :unpack _G.unpack))
+
+(fn ends-with [str end]
+ (= (string.sub str (- (# end))) end))
+
+(fn trim [str pattern]
+ (local pattern (or pattern "%s"))
+ (str:match (.. "^" pattern "*(.-)" pattern "*$")))
+
+(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")))
+
+(luna.router.route "GET /" router)
+(luna.router.static "GET /static/" "static/")