(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 (.. "\n" html))) (values 404 {:content-type "text/html"} "not found"))) (luna.router.route "GET /" router) (luna.router.static "GET /static/" "static/")