(local fennel (require :vendor.fennel)) (when _G.unpack (tset table :unpack _G.unpack)) (set _G.reload (fn [module] (local old (require module)) (tset package :loaded module nil) (local (ok? new) (pcall require module)) (if (not ok?) (do (tset package :loaded module old) (error new)) (when (= (type new) :table) (do (each [k v (pairs new)] (tset old k v)) (each [k (pairs old)] (when (not (. new k)) (tset old k nil))) (tset package :loaded module old)))))) (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/") (when luna.debug (luna.evalfn (fn [code] (fennel.eval code {:env _G}))))