rust编程语言的Web框架:Nickel.rs
jopen
10年前
Nickel.rs是rust编程语言的一个Web应用程序开发框架。它包含一个自定义错误处理器,JSON处理等。
Your first nickel app
extern crate http; extern crate nickel; use std::io::net::ip::Ipv4Addr; use nickel::{ Nickel, Request, Response }; fn main() { let mut server = Nickel::new(); fn a_handler (_request: &Request, response: &mut Response) { response.send("hello world"); } server.get("/bar", a_handler); server.listen(Ipv4Addr(127, 0, 0, 1), 6767); }
Flexible routing
Routes can be as simple as /foo
, use parameters, wildcards or even double wildcards.
extern crate http; extern crate nickel; use std::io::net::ip::Ipv4Addr; use nickel::{ Nickel, Request, Response }; fn main() { let mut server = Nickel::new(); fn handler_with_param (request: &Request, response: &mut Response) { let text = format!("This is user: {}", request.params.get(&"userid".to_string())); response.send(text.as_slice()); } fn simple_handler (_request: &Request, response: &mut Response) { response.send("This is the /bar handler"); } fn simple_wildcard (_request: &Request, response: &mut Response) { response.send("matches /a/b/d but not /a/b/c/d"); } fn double_wildcard (_request: &Request, response: &mut Response) { response.send("This matches /a/b/d and also /a/b/c/d"); } server.get("/bar", simple_handler); server.get("/user/:userid", handler_with_param); server.get("/a/*/d", simple_wildcard); server.get("/a/**/d", double_wildcard); server.listen(Ipv4Addr(127, 0, 0, 1), 6767); }
Middleware
Middleware are the extensibility points of nickel. Batteries included! A bunch of existing Middleware comes right with nickel. Such as the StaticFileHandler
, Logger
or JsonBodyParser
Middleware
extern crate http; extern crate nickel; use std::io::net::ip::Ipv4Addr; use nickel::{ Nickel, Request, Response }; fn main() { let mut server = Nickel::new(); server.utilize(Nickel::static_files("examples/assets/")); server.utilize(Nickel::json_body_parser()); server.listen(Ipv4Addr(127, 0, 0, 1), 6767); }
JSON handling
nickel makes it easy to map JSON data right onto you struct
.
extern crate http; extern crate serialize; extern crate nickel; use std::io::net::ip::Ipv4Addr; use nickel::{ Nickel, Request, Response }; #[deriving(Decodable, Encodable)] struct Person { firstname: String, lastname: String, } fn main() { let mut server = Nickel::new(); server.utilize(Nickel::json_body_parser()); fn post_handler (request: &Request, response: &mut Response) { let person = request.json_as::<Person>().unwrap(); let text = format!("Hello {} {}", person.firstname, person.lastname); response.send(text.as_slice()); } server.post("/a/post/request", post_handler); server.listen(Ipv4Addr(127, 0, 0, 1), 6767); }
Custom error handler
By default nickel catches all errors with it's default ErrorHandler
and tries to take reasonable actions. In cases where one wants to provide an own ErrorHandler
(e.g. for custom 404 pages), it's trivial to write one.
extern crate http; extern crate serialize; extern crate nickel; use http::status::NotFound; use std::io::net::ip::Ipv4Addr; use nickel::{ Nickel, NickelError, ErrorWithStatusCode, Action, Continue, Halt, Request, Response, IntoErrorHandler, }; fn main() { let mut server = Nickel::new(); fn custom_404 (err: &NickelError, _req: &Request, response: &mut Response) -> Result<Action, NickelError> { match err.kind { ErrorWithStatusCode(NotFound) => { response.set_content_type("html"); response.origin.status = NotFound; response.send("<h1>Call the police!<h1>"); Ok(Halt) }, _ => Ok(Continue) } } server.handle_error(IntoErrorHandler::from_fn(custom_404)); server.listen(Ipv4Addr(127, 0, 0, 1), 6767); }