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
options: Options
Configuration options for the server.
Methods
impl Nickel<()>
[src]
impl<D: Sync + Send + 'static> Nickel<D>
[src]
fn with_data(data: D) -> Nickel<D>
Creates an instance of Nickel with default error handling and custom data.
fn utilize<T: Middleware<D>>(&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<D>>(&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, 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)
fn router() -> Router<D>
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
) -> Result<ListeningServer, Box<StdError>>
self,
addr: T
) -> Result<ListeningServer, Box<StdError>>
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());
fn keep_alive_timeout(&mut self, timeout: Option<Duration>)
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.
fn listen_https<T, S>(
self,
addr: T,
ssl: S
) -> Result<ListeningServer, Box<StdError>> where
T: ToSocketAddrs,
S: SslServer + Send + Clone + 'static,
self,
addr: T,
ssl: S
) -> Result<ListeningServer, Box<StdError>> where
T: ToSocketAddrs,
S: SslServer + Send + Clone + 'static,
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 add_route<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
method: Method,
matcher: M,
handler: H
) -> &mut Self
&mut self,
method: Method,
matcher: M,
handler: H
) -> &mut Self
Registers a handler to be used for a specified method. A handler can be anything implementing the RequestHandler
trait. Read more
fn get<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
&mut self,
matcher: M,
handler: H
) -> &mut Self
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
&mut self,
matcher: M,
handler: H
) -> &mut Self
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
&mut self,
matcher: M,
handler: H
) -> &mut Self
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
&mut self,
matcher: M,
handler: H
) -> &mut Self
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
&mut self,
matcher: M,
handler: H
) -> &mut Self
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
&mut self,
matcher: M,
handler: H
) -> &mut Self
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]
D: Send + Sync + 'static,
fn mount<S: Into<String>, M: Middleware<D>>(
&mut self,
mount_point: S,
middleware: M
)
&mut self,
mount_point: S,
middleware: M
)
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.