[][src]Struct nickel::Nickel

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

options: Options

Configuration options for the server.

Methods

impl Nickel<()>
[src]

pub fn new() -> Nickel<()>
[src]

Creates an instance of Nickel with default error handling.

pub fn with_options(options: Options) -> Nickel<()>
[src]

Creates and instance of Nickel with custom Options.

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

pub fn with_data_and_options(data: D, options: Options) -> Nickel<D>
[src]

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

pub fn with_data(data: D) -> Nickel<D>
[src]

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

pub fn utilize<T: Middleware<D>>(&mut self, handler: T)
[src]

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

pub fn handle_error<T: ErrorHandler<D>>(&mut self, handler: T)
[src]

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)

pub fn router() -> Router<D>
[src]

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

pub fn listen<T: ToSocketAddrs>(
    self,
    addr: T
) -> Result<ListeningServer, Box<dyn StdError>>
[src]

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

pub fn keep_alive_timeout(&mut self, timeout: Option<Duration>)
[src]

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.

pub fn listen_https<T, S>(
    self,
    addr: T,
    ssl: S
) -> Result<ListeningServer, Box<dyn StdError>> where
    T: ToSocketAddrs,
    S: SslServer + Send + Clone + 'static, 
[src]

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]

fn get<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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

fn post<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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

fn put<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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

fn delete<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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

fn options<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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

fn patch<M: Into<Matcher>, H: Middleware<D>>(
    &mut self,
    matcher: M,
    handler: H
) -> &mut Self
[src]

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]

fn mount<S: Into<String>, M: Middleware<D>>(
    &mut self,
    mount_point: S,
    middleware: M
)
[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.

Auto Trait Implementations

impl<D> Send for Nickel<D>

impl<D> Sync for Nickel<D>

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Typeable for T where
    T: Any

fn get_type(&self) -> TypeId

Get the TypeId of this object.

impl<T> UnsafeAny for T where
    T: Any