(sigil web routes)
(sigil web routes) - Path-based Request Routing
Provides routing functionality for web applications. Routes are matched in order; first match wins.
Example: (define app (router (route method: GET pattern: "/" handler: home-handler) (route method: GET pattern: "/users/:id" handler: user-handler) (route method: POST pattern: "/api/login" handler: login-handler) (route method: ANY pattern: "/static/*path" handler: static-handler)))
method: defaults to POST when omitted.
Exports
routeprocedureA route consists of a method, pattern, and handler.
method: is an HTTP-method symbol (GET, POST, ANY, ...) and defaults to POST when omitted. A "post" string is accepted too and normalized at match time.
route?procedureTest if a value is a route struct.
route-methodprocedureGet the method field of a route struct.
route-patternprocedureGet the pattern field of a route struct.
route-handlerprocedureGet the handler field of a route struct.
parse-patternprocedureParse a route pattern into segments Returns list of: string (literal), (:param name), (*splat name)
parse-segmentprocedureParse a single path segment
match-patternprocedureMatch a path against a parsed pattern Returns alist of params on match, #f on no match
match-segmentsprocedureMatch path segments against pattern segments
normalize-methodprocedureNormalize an HTTP method to an upcased symbol. Accepts a symbol ('GET, 'post) or string ("get", "POST").
method-matches?procedureDoes a route's method match a request's method? 'ANY matches everything; otherwise compare normalized methods so symbol/string and case differences don't matter.
make-routerprocedureCreate a router from a list of routes Returns a handler function: (request) -> response or #f
routerprocedureConvenience function for creating routers. Usage: (router (route method: GET pattern: "/" handler: handler1) (route method: POST pattern: "/api" handler: handler2))
path-paramprocedureGet a path parameter from request Returns value or #f if not found
path-paramsprocedureGet all path parameters from request
routesprocedureCombine multiple handlers into a single handler.
Returns the first non-#f response. Use with the threading macro to build middleware chains:
(-> (routes api-routes page-routes static-handler)
(with-logging)
(with-not-found))GETvariable(No description)
POSTvariable(No description)
PUTvariable(No description)
DELETEvariable(No description)
PATCHvariable(No description)
OPTIONSvariable(No description)
HEADvariable(No description)
ANYvariable(No description)