sigildocs

(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

routeprocedure

A 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?procedure

Test if a value is a route struct.

route-methodprocedure

Get the method field of a route struct.

route-patternprocedure

Get the pattern field of a route struct.

route-handlerprocedure

Get the handler field of a route struct.

parse-patternprocedure

Parse a route pattern into segments Returns list of: string (literal), (:param name), (*splat name)

parse-segmentprocedure

Parse a single path segment

match-patternprocedure

Match a path against a parsed pattern Returns alist of params on match, #f on no match

Match path segments against pattern segments

Normalize an HTTP method to an upcased symbol. Accepts a symbol ('GET, 'post) or string ("get", "POST").

Does 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-routerprocedure

Create a router from a list of routes Returns a handler function: (request) -> response or #f

routerprocedure

Convenience function for creating routers. Usage: (router (route method: GET pattern: "/" handler: handler1) (route method: POST pattern: "/api" handler: handler2))

path-paramprocedure

Get a path parameter from request Returns value or #f if not found

path-paramsprocedure

Get all path parameters from request

routesprocedure

Combine 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)

ANYvariable

(No description)