Struct nickel::Nickel [] [src]

pub struct Nickel {
    // some fields omitted
}

Nickel is the application object. It's the surface that holds all public APIs.

Methods

impl Nickel

fn new() -> Nickel

Creates an instance of Nickel with default error handling.

fn utilize<T: Middleware>(&mut self, handler: T)

Registers a middleware handler which will be invoked among other middleware handlers before each request. Middleware can be stacked and is invoked in the same order it was registered.

A middleware handler is nearly identical to a regular route handler with the only difference that it expects a result of either Action or NickelError. That is to indicate whether other middleware handlers (if any) further down the stack should continue or if the middleware invocation should be stopped after the current handler.

Examples

use nickel::Nickel;
let mut server = Nickel::new();
server.utilize(middleware! { |req|
    println!("logging request: {:?}", req.origin.uri);
});

fn handle_error<T: ErrorHandler>(&mut self, handler: T)

Registers an error handler which will be invoked among other error handler as soon as any regular handler returned an error

A error handler is nearly identical to a regular middleware handler with the only difference that it takes an additional error parameter or type `NickelError.

Examples

use std::io::Write;
use nickel::{Nickel, Request, Response, Continue, Halt};
use nickel::{NickelError, Action};
use nickel::status::StatusCode::NotFound;

fn error_handler(err: &mut NickelError, req: &mut Request) -> Action {
   if let Some(ref mut res) = err.stream {
       if res.status() == NotFound {
           let _ = res.write_all(b"<h1>Call the police!</h1>");
           return Halt(())
       }
   }

    Continue(())
}

let mut server = Nickel::new();

let ehandler: fn(&mut NickelError, &mut Request) -> Action = error_handler;

server.handle_error(ehandler)

fn router() -> Router

Create a new middleware to serve as a router.

Examples

#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter};

fn main() {
    let mut server = Nickel::new();
    let mut router = Nickel::router();

    router.get("/foo", middleware! {
        "Hi from /foo"
    });

    server.utilize(router);
}

fn listen<T: ToSocketAddrs>(self, addr: T)

Bind and listen for connections on the given host and port.

Panics

Panics if addr is an invalid SocketAddr.

Examples

use nickel::Nickel;

let mut server = Nickel::new();
server.listen("127.0.0.1:6767");

Trait Implementations

impl HttpRouter for Nickel

fn add_route<M: Into<Matcher>, H: Middleware>(&mut self, method: Method, matcher: M, handler: H)

fn get<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)

fn post<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)

fn put<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)

fn delete<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)

fn options<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)

fn patch<M: Into<Matcher>, H: Middleware>(&mut self, matcher: M, handler: H)