mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Make routing constraints use the fields format
This makes routing more in line with the rest of Cowboy and allows us to use cowboy_constraints directly.
This commit is contained in:
parent
f1c3b6d76f
commit
25259671f5
3 changed files with 36 additions and 70 deletions
|
@ -191,26 +191,16 @@ HostMatch = "*".
|
|||
After the matching has completed, the resulting bindings can be tested
|
||||
against a set of constraints. Constraints are only tested when the
|
||||
binding is defined. They run in the order you defined them. The match
|
||||
will succeed only if they all succeed.
|
||||
will succeed only if they all succeed. If the match fails, then Cowboy
|
||||
tries the next route in the list.
|
||||
|
||||
They are always given as a two or three elements tuple, where the first
|
||||
element is the name of the binding, the second element is the constraint's
|
||||
name, and the optional third element is the constraint's arguments.
|
||||
The format used for constraints is the same as match functions in
|
||||
`cowboy_req`: they are provided as a list of fields which may have
|
||||
one or more constraints. While the router accepts the same format,
|
||||
it will skip fields with no constraints and will also ignore default
|
||||
values, if any.
|
||||
|
||||
The following constraints are currently defined:
|
||||
|
||||
* {Name, int}
|
||||
* {Name, function, fun ((Value) -> true | {true, NewValue} | false)}
|
||||
|
||||
The `int` constraint will check if the binding is a binary string
|
||||
representing an integer, and if it is, will convert the value to integer.
|
||||
|
||||
The `function` constraint will pass the binding value to a user specified
|
||||
function that receives the binary value as its only argument and must
|
||||
return whether it fulfills the constraint, optionally modifying the value.
|
||||
The value thus returned can be of any type.
|
||||
|
||||
Note that constraint functions SHOULD be pure and MUST NOT crash.
|
||||
Read more about ^constraints^.
|
||||
|
||||
:: Compilation
|
||||
|
||||
|
|
|
@ -22,31 +22,16 @@ Environment output:
|
|||
|
||||
List of bindings found during routing.
|
||||
|
||||
: constraints() = [IntConstraint | FunConstraint]
|
||||
|
||||
Types:
|
||||
|
||||
* IntConstraint = {atom(), int}
|
||||
* FunConstraint = {atom(), function, Fun}
|
||||
* Fun = fun((binary()) -> true | {true, any()} | false)
|
||||
|
||||
List of constraints to apply to the bindings.
|
||||
|
||||
The int constraint will convert the binding to an integer.
|
||||
The fun constraint allows writing custom code for checking
|
||||
the bindings. Returning a new value from that fun allows
|
||||
replacing the current binding with a new value.
|
||||
|
||||
: dispatch_rules() - opaque to the user
|
||||
|
||||
Rules for dispatching request used by Cowboy.
|
||||
|
||||
: routes() = [{Host, Paths} | {Host, constraints(), Paths}]
|
||||
: routes() = [{Host, Paths} | {Host, cowboy:fields(), Paths}]
|
||||
|
||||
Types:
|
||||
|
||||
* Host = Path = '_' | iodata()
|
||||
* Paths = [{Path, Handler, Opts} | {Path, constraints(), Handler, Opts}]
|
||||
* Paths = [{Path, Handler, Opts} | {Path, cowboy:fields(), Handler, Opts}]
|
||||
* Handler = module()
|
||||
* Opts = any()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue