Radix-Router
Radix-Router is a Rust port of julienschmidt/httprouter.
Usage
This is just a quick introduction.
Let's start with a hello world
example:
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate radix_router;
use future;
use ;
use ;
use ;
Handler
The handler can be anything. You can store a T
and get an Option<&T>
. Notice that &T
is immutable. We offer a default radix_router::router::Handler
which can be a fn
or closure
. When using closure, you are able to capture outside parameters. For example:
router.get;
router.post;
router.post;
router.post;
router.get;
Named parameters
:name
is a named parameter. The values are accessible via Option<Params>
, which is a wrapped slice of Param
s. You can get the value of a parameter either by its index in the slice. of by using the by_name(name)
method.
Named parameters only match a single path segment:
Pattern: /user/:user
/user/gordon match
/user/you match
/user/gordon/profile no match
/user/ no match
Note: Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns /user/new
and /user/:user
for the same request method at the same time. The routing of different request methods is independent from each other.
Catch-All parameters
The second type are catch-all parameters and have the form *name
. Like the name suggests, they match everything. Therefore they must always be at the end of the pattern:
Pattern: /src/*filepath
/src/ match
/src/somefile.go match
/src/subdir/somefile.go match
Static files
You can serve static files by using:
router.serve_files;
Examples
An echo server example is written. You can test it by running
param1=1¶m2=2