-
-
Save DAddYE/10066670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HTTPRouter | |
self() | |
self.routes = { } | |
end | |
add(verb = String, path = String, func = Function) | |
self.routes[#self.routes + 1] = Route(verb, path, func) | |
end | |
match(verb, path) | |
for i=1, #self.routes do | |
m, q = self.routes[i].match(verb, path) | |
if m then | |
return self.routes[i], m, q | |
end | |
end | |
end | |
class Route | |
local frag_patt = / '/' %1 { [^/?]+ } / | |
self(verb, path, func) | |
self.handler = func | |
self.verb = verb | |
self.path = path | |
self.patt = / / | |
for frag in path.gmatch("(:?[^/]*)") do | |
if frag == '' then | |
continue | |
end | |
if frag[1] == ':' then | |
-- named path parameter | |
name = frag[2..-1] | |
self.patt *= frag_patt % (a, v) => | |
a[name] = v | |
end | |
else | |
-- literal match | |
self.patt *= / '/' <{frag}> / | |
end | |
end | |
-- collect the remaining path fragments | |
self.patt *= / <{frag_patt}> -> (a, v) => | |
a[#a + 1] = v | |
end / ** 0 | |
-- collect the remaining query part | |
self.patt *= / %1 ('/'? {.+})? / | |
end | |
match(verb, path) | |
if self.verb != verb then | |
return nil | |
end | |
return self.patt.match(path, 1, { }) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment