Canteen
Description
Canteen is the first project that I'm implementing in Rust. It's a simple clone of
Flask, my very favorite Python web framework. The code for
the library is located above in the rust-canteen
directory, and there is code for
an example implementation in the canteen-impl
repository.
Usage
It's by no means complete, but I'm working on it, and it's now available on crates.io! To install and check it out, add the following to your Cargo.toml:
[]
= "0.1"
Example
The principle behind Canteen is simple -- handler functions are defined as simple
Rust functions that take a Request
and return a Response
. Handlers are then attached
to one or more routes and HTTP methods/verbs. Routes are specified using a simple
syntax that lets you define variables within them; variables that can then be
extracted to perform various operations. Currently, the following variable types can
be used:
<path:name>
will greedily take all path data contained- ex:
cnt.add_route("/static/<path:name>", vec![Method::Get], Route::static_file)
will serve anything in the/static/
directory as a file
- ex:
<int:name>
will return an integer from a path segment- ex:
cnt.add_route("/api/foo/<int:foo_id>", vec![Method::Get], my_handler)
will match"/api/foo/123"
but not"/api/foo/123.34"
or"/api/foo/bar"
- ex:
<float:name>
does the same thing as theint
parameter definition, but matches numbers with decimal points<str:name>
will match anything inside a path segment, except a forward slash
extern crate canteen;
use *;