summaryrefslogtreecommitdiff
path: root/web/router.lua
diff options
context:
space:
mode:
authorrebecca <ubq323@ubq323.website>2026-09-17 20:54:44 +0100
committerrebecca <ubq323@ubq323.website>2026-09-17 20:54:44 +0100
commit6c5d5fec129a29c6250b094f46881cfa5aaadcc7 (patch)
treeb66161fccfa91180e937f0689763f2124a4af97b /web/router.lua
parent7f60b7c4e8d99f97a0a24b60bd4c4fd15697a4bb (diff)
web: import from m1msq 15b348f
Diffstat (limited to 'web/router.lua')
-rw-r--r--web/router.lua20
1 files changed, 20 insertions, 0 deletions
diff --git a/web/router.lua b/web/router.lua
new file mode 100644
index 0000000..09be12a
--- /dev/null
+++ b/web/router.lua
@@ -0,0 +1,20 @@
+local fail = require'lib.fail'
+local class = require'r.class'
+local pprint = require'pprint'
+local Router = class()
+function Router.make(cls) return setmetatable({routes={}},cls) end
+function Router._add(self, patt,israw,view)
+ table.insert(self.routes,{patt=patt,israw=israw,view=view}) end
+function Router.path(self, path, view) self:_add(path,true,view) end
+function Router.patt(self, patt, view) self:_add(patt,false,view) end
+-- todo: recursion
+function Router.__call(self, req) for _,r in ipairs(self.routes) do
+ local s = { req.path:find(r.patt,1,r.israw) }
+ if s[1] then return r.view(req, table.unpack(s,3)) end end
+ fail(404)
+end
+
+return Router
+
+-- view protocol:
+-- view: callable(req) -> (body,headers,status)