Common Lisp Package: CLACK.APP.ROUTE

# Clack.App.Route - URL dispatcher. ## SYNOPSIS (defpackage clack-sample (:use :cl :clack :clack.app.route)) (in-package :clack-sample) (defroutes app (GET "/" #'index) (GET "/login" #'login) (POST "/login" #'authorize) (GET "/member/:id" #'member)) (clackup #'app) ## DESCRIPTION Clack.App.Route provides an URL based dispacher, inspired by Ruby's Sinatra. Each routing rules contains three elements, REQUEST-METHOD, URI rule and Clack Application. Clack Application is a function or Clack.Component which takes exact one argument. It is a property list represents Environment. (defroutes app (GET "/" (lambda (env) '(200 (:content-type "text/plain") ("Hello. This is an index page."))))) Routing URI rules are also allowed to contain named parameters or wild cards. Then, Clack.App.Route adds `:route.parameters` to `env`. (defroutes app (GET "/member/:id" (lambda (env) (destructuring-bind (&key id &allow-other-keys) (getf env :route.parameters) `(200 (:content-type "text/plain") (,(format nil "Your member ID is ~D" id)))))) (GET "/say/*/to/*" (lambda (env) (destructuring-bind (who what) (getf (getf env :route.parameters) :splat) `(200 (:content-type "text/plain") (,(format nil "You're saying ~A to ~A" what who))))))) This package is using Clack.Util.Route to parse rule strings. Note: Though `parse-url-rule` is provided from this package now, it is now deprecated. Use Clack.Util.Route for instead. ## AUTHOR * Tomohiro Matsuyama (tomo@cx4a.org) ## CONTRIBUTORS * Eitarow Fukamachi (e.arrows@gmail.com) ## SEE ALSO * Clack.Util.Route

README:

FUNCTION

Public

PARSE-URL-RULE (URL)

[DEPRECATED] Parse an URL rule and return a list of (regex control-string variables). Example: (parse-url-rule "/login") ;;=> ("^\/login$" "/login" NIL) (parse-url-rule "/member/:id") ;;=> ("^\/member\/(.+?)$" "/member/~A" (ID))

Private

MAKE-URL-RULE (URL &KEY (METHOD GET) REGEXP)

Construct `<url-rule>` and return it. You must always use this function when you need `<url-rule>`.

QUOTE-META-CHARS (STRING &KEY (START 0) (END (LENGTH STRING)))

Quote, i.e. prefix with #\\, all non-word characters in STRING.

SCAN-TO-STRINGS (REGEX TARGET-STRING &KEY (START 0) (END (LENGTH TARGET-STRING)) SHAREDP)

Like SCAN but returns substrings of TARGET-STRING instead of positions, i.e. this function returns two values on success: the whole match as a string plus an array of substrings (or NILs) corresponding to the matched registers. If SHAREDP is true, the substrings may share structure with TARGET-STRING.

SPLIT (REGEX TARGET-STRING &KEY (START 0) (END (LENGTH TARGET-STRING)) LIMIT WITH-REGISTERS-P OMIT-UNMATCHED-P SHAREDP)

Matches REGEX against TARGET-STRING as often as possible and returns a list of the substrings between the matches. If WITH-REGISTERS-P is true, substrings corresponding to matched registers are inserted into the list as well. If OMIT-UNMATCHED-P is true, unmatched registers will simply be left out, otherwise they will show up as NIL. LIMIT limits the number of elements returned - registers aren't counted. If LIMIT is NIL (or 0 which is equivalent), trailing empty strings are removed from the result list. If REGEX matches an empty string the scan is continued one position behind this match. If SHAREDP is true, the substrings may share structure with TARGET-STRING.

MACRO

Public

Undocumented

DEFROUTES (NAME &BODY ROUTES &AUX (OTHERWISE (LAST ROUTES)))

Private

DOC (DOCSTRING DEFINITION-FORM)

Add DOCSTRING documentation for DEFINITION-FORM.

WITH-GENSYMS (NAMES &BODY FORMS)

Binds each variable named by a symbol in NAMES to a unique symbol around FORMS. Each of NAMES must either be either a symbol, or of the form: (symbol string-designator) Bare symbols appearing in NAMES are equivalent to: (symbol symbol) The string-designator is used as the argument to GENSYM when constructing the unique symbol the named variable will be bound to.

Undocumented

EVAL-ALWAYS (&BODY BODY)

GENERIC-FUNCTION

Private

Undocumented

MATCH (THIS METHOD URL-STRING &KEY ALLOW-HEAD)