Files
playlet-piped/src/components/WebServer/HttpRouter.bs
T
Brahim Hadriche ed7c5c618b Feature/web socket (#25)
* Add basic handshake

* Basic message exchange

* Base for a simple socket server

* Web socket events

* changelog

* Small tweak
2022-12-08 17:31:43 -05:00

66 lines
2.1 KiB
Plaintext

namespace Http
class HttpRouter
private routes
function new()
m.routes = []
end function
function Get(path as string, handler as function)
m.routes.push({ method: "GET", path: path, handler: handler })
end function
function Head(path as string, handler as function)
m.routes.push({ method: "HEAD", path: path, handler: handler })
end function
function Post(path as string, handler as function)
m.routes.push({ method: "POST", path: path, handler: handler })
end function
function Put(path as string, handler as function)
m.routes.push({ method: "PUT", path: path, handler: handler })
end function
function Delete(path as string, handler as function)
m.routes.push({ method: "DELETE", path: path, handler: handler })
end function
function All(path as string, handler as function)
m.routes.push({ method: "*", path: path, handler: handler })
end function
private function OnRequest(request as object, response as HttpResponse, connection as HttpConnection) as boolean
for each route in m.routes
if m.RouteMatchesRequest(route, request)
if route.handler({
router: m,
request: request,
response: response,
connection: connection,
server: response.server
}) = true
return true
end if
end if
end for
return false
end function
private function RouteMatchesRequest(route as object, request as HttpRequest) as boolean
if route.method <> "*" and route.method <> request.method
return false
end if
if route.path <> "*" and route.path <> request.route
return false
end if
return true
end function
end class
end namespace