7.7
routy
(require routy) | package: routy |
HTTP router for Racket
Routy is a lightweight high performance HTTP request router for Racket. It uses the same routing syntax as used by popular Ruby web frameworks like Ruby on Rails and Sinatra.
1 API
procedure
(routy/get path proc [ #:constraints constraints]) → response? path : string? proc : (-> (or/c response? string?)) constraints : (listof pair?) = '()
handle GET request
path - path pattern. eg. "/blog/some*/page/:page"
proc - procedure takes 2 arguments: (req params)
req (request?)- HTTP request
params (listof pair?) - requestr params, should be used with request/param
returns string or response
(routy/get "/blog/:name/page/:page" (lambda (req params) (format "blog:~a page:~a" (request/param params 'name) (request/param params 'page))))
procedure
(routy/post path proc [ #:constraints constraints]) → void? path : string? proc : (-> (request? (listof pair?)) (or/c response? string?)) constraints : (listof pair?) = '()
handle POST request
procedure
(routy/put path proc [ #:constraints constraints]) → void? path : string? proc : (-> (or/c response? string?)) constraints : (listof pair?) = '()
handle PUT request
procedure
(routy/patch path proc [ #:constraints constraints]) → void? path : string? proc : (-> (or/c response? string?)) constraints : (listof pair?) = '()
handle PATCH request
procedure
(routy/delete path proc [ #:constraints constraints]) → void? path : string? proc : (-> (or/c response? string?)) constraints : (listof pair?) = '()
handle DELETE request
procedure
(routy/files path [#:root root]) → void?
path : path-string? root : string? = (current-directory)
serve files
procedure
(routy/not-found content) → void?
content : (or/c string? procedure?)
Not found route
procedure
(routy/response req) → response?
req : request?
Handle web-server requests.
(serve/servlet (λ (req) (routy/response req)) #:launch-browser? #f #:servlet-path "/" #:port 8000 #:servlet-regexp #rx"")
2 Example of usage
(require routy) (require web-server/servlet) (require response-ext) (routy/get "/blog/:name/page/:page" (lambda (req params) (format "blog:~a page:~a" (request/param params 'name) (request/param params 'page)))) (routy/get "/product/:id" (lambda (req params) (response/not-found))) (routy/post ...) (routy/put ...) (routy/delete ...) (routy/patch ...) (serve/servlet (λ (req) (routy/response req)) #:launch-browser? #f #:servlet-path "/" #:port 8000 #:servlet-regexp #rx"") (routy/get "/blog/some*/page/:page" (lambda (req params) (format "page:~a" (request/param params 'page)))) (routy/get "/blog/*/page/:page" (lambda (req params) (format "page:~a" (request/param params 'page)))) (routy/get "/blog/**/page/:page" (lambda (req params) (format "page:~a" (request/param params 'page)))) (routy/not-found (lambda (req) "OOPS.. CANNOT FIND THIS PAGE")) (routy/not-found "OOPS.. CANNOT FIND THIS PAGE") (routy/not-found (lambda (req) (response/make #:code 200 "SOME TEXT"))) (routy/files "/plain-docs" #:root "/var/www/my-site") (routy/get "/blog/:name/page/:page" #:constraints '((name #px"\\w+") (page #px"\\d+")) (lambda (req params) (format "blog:~a page:~a" (request/param params 'name) (request/param params 'page))))