Struct nickel::Nickel [] [src]

pub struct Nickel<D: Sync + Send + 'static = ()> {
    pub options: Options,
    // some fields omitted
}

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

Fields

Configuration options for the server.

Methods

impl Nickel<()>
[src]

Creates an instance of Nickel with default error handling.

impl<D: Sync + Send + 'static> Nickel<D>
[src]

Creates an instance of Nickel with default error handling and custom data.

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);
});

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, Continue, Halt};
use nickel::{NickelError, Action};
use nickel::status::StatusCode::NotFound;

fn error_handler<D>(err: &mut NickelError<D>, _req: &mut Request<D>) -> 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)

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);
}

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

Examples

use nickel::Nickel;

let server = Nickel::new();
let listening = server.listen("127.0.0.1:6767").expect("Failed to launch server");
println!("Listening on: {:?}", listening.socket());

Set the timeout for the keep-alive loop

Performance

Setting this to None can have significant performance impact, but if you need to use a version of rustc < 1.4, then it may be a good choice.

Alternatively, setting this too high, can lead to thread exhaustion, see this thread for more.

Default

The default value is 75 seconds.

Bind and listen for connections on the given host and port. Only accepts SSL connections

Panics

Panics if addr is an invalid SocketAddr.

Examples

extern crate hyper;

use nickel::Nickel;
use hyper::net::Openssl;

let server = Nickel::new();
let ssl = Openssl::with_cert_and_key("foo.crt", "key.pem").unwrap();
server.listen_https("127.0.0.1:6767", ssl).unwrap();

Trait Implementations

impl<D: Sync + Send + 'static> HttpRouter<D> for Nickel<D>
[src]

Registers a handler to be used for a specified method. A handler can be anything implementing the RequestHandler trait. Read more

Registers a handler to be used for a specific GET request. Handlers are assigned to paths and paths are allowed to contain variables and wildcards. Read more

Registers a handler to be used for a specific POST request. Read more

Registers a handler to be used for a specific PUT request. Read more

Registers a handler to be used for a specific DELETE request. Read more

Registers a handler to be used for a specific OPTIONS request. Read more

Registers a handler to be used for a specific PATCH request. Read more

impl<D> Mountable<D> for Nickel<D> where
    D: Send + Sync + 'static, 
[src]

A trait that makes mounting more convenient. Works the same as manually adding a Mount middleware.

Examples

use nickel::{Nickel, StaticFilesHandler, Mountable};
let mut server = Nickel::new();

server.mount("/static_files/", StaticFilesHandler::new("/path/to/serve/"));

Panics

Panics if mount_point does not have a leading and trailing slash.