Skip to content

Commit

Permalink
Generalised the route handler
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Sep 23, 2022
1 parent 82e9b89 commit c709186
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
42 changes: 10 additions & 32 deletions defnet/http_router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,70 +34,47 @@ function M.create()
end

function instance.match(method, uri)
local response
if uri then
for _,route in ipairs(routes) do
if not route.method or route.method == method then
local matches = { uri:match(route.pattern) }
if next(matches) then
response = route.fn(matches, stream_fn, headers, message_body)
break
return true, route.fn, matches
end
end
end
end

-- unhandled response
if not response and unhandled_route_fn then
response = unhandled_route_fn(method, uri, stream_fn, headers, message_body)
end

return response
return false, unhandled_route_fn
end

return instance
end

--- Route HTTP GET requests matching a specific pattern to a
-- provided function.
-- The function will receive a list of matches from the pattern as
-- it's first arguments. The second argument is a stream function in case
-- the response should be streamed.
-- The function must either return the full response or a function that
-- can be called multiple times to get more data to return.
-- @param router
-- @param pattern Standard Lua pattern
-- @param fn Function to call
-- @param fn Function to call when this route is matched
function M.get(router, pattern, fn)
assert(router)
return router.get(pattern, fn)
end

--- Route HTTP POST requests matching a specific pattern to a
-- provided function.
-- The function will receive a list of matches from the pattern as
-- it's first arguments. The second argument is a stream function in case
-- the response should be streamed.
-- The function must either return the full response or a function that
-- can be called multiple times to get more data to return.
-- @param router
-- @param pattern Standard Lua pattern
-- @param fn Function to call
-- @param fn Function to call when this route is matched
function M.post(router, pattern, fn)
assert(router)
return router.post(pattern, fn)
end

--- Route all HTTP requests matching a specific pattern to a
-- provided function.
-- The function will receive a list of matches from the pattern as
-- it's first arguments. The second argument is a stream function in case
-- the response should be streamed.
-- The function must either return the full response or a function that
-- can be called multiple times to get more data to return.
-- @param router
-- @param pattern Standard Lua pattern
-- @param fn Function to call
-- @param fn Function to call when this route is matched
function M.all(pattern, fn)
assert(router)
return router.all(pattern, fn)
Expand All @@ -106,19 +83,20 @@ end
--- Add a handler for unhandled routes. This is typically where
-- you would return a 404 page
-- @param router
-- @param fn The function to call when an unhandled route is encountered. The
-- function will receive the method and uri of the unhandled route as
-- @param fn The function to call when an unhandled route is encountered
-- arguments.
function M.unhandled(router, fn)
assert(router)
return router.unhandled(fn)
end

--- Match a method and uri with a route. If no match exists the unhandled route function is used
--- Match a method and uri with a route.
-- @param router
-- @param method
-- @param uri
-- @return response
-- @return handled Boolean indicating if the route was handled or not
-- @return fn Route function, or the unhandled route function if no route was found
-- @return matches Any matches captured from the route pattern
function M.match(router, method, uri)
assert(router)
return router.match(method, uri)
Expand Down
27 changes: 23 additions & 4 deletions defnet/http_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ end


--- Create a new HTTP server
-- Use start(), stop() and update() on the returned instance
-- Set http route functions to react to incoming requests
--
-- A note on route functions:
-- The function will receive a list of matches from the pattern as
-- it's first arguments. The second argument is a stream function in case
-- the response should be streamed.
-- The function must either return the full response or a function that
-- can be called multiple times to get more data to return.
--
-- @return Server instance
function M.create(port)
local instance = {
Expand Down Expand Up @@ -159,7 +169,13 @@ function M.create(port)
end

-- handle request and get a response
local response = http_router.match(instance.router, method, uri)
local response
local handled, fn, matches = instance.router.match(method, uri)
if handled then
response = fn(matches, stream_fn, headers, message_body)
elseif not handled and fn then
response = fn(method, uri, stream_fn, headers, message_body)
end

-- send response
if response then
Expand Down Expand Up @@ -188,14 +204,17 @@ function M.create(port)
function instance.update()
ss.update()
for k,handler in pairs(request_handlers) do
print("calling request handler")
if not handler() then
print("removing request handler")
request_handlers[k] = nil
end
end
end

function instance.set_router(router)
assert(router)
instance.router = router
end

--- Return a properly formatted HTML response with the
-- appropriate response headers set
-- If the document is omitted the response is assumed to be
Expand Down Expand Up @@ -310,7 +329,7 @@ end
function M.set_router(server, router)
assert(server)
assert(router)
server.router = router
return server.set_router(router)
end

return M

0 comments on commit c709186

Please sign in to comment.