Expand description
Nickel is the application object. It’s the surface that holds all public APIs.
Fields
options: Options
Configuration options for the server.
Implementations
sourceimpl<D: Sync + Send + 'static> Nickel<D>
impl<D: Sync + Send + 'static> Nickel<D>
sourcepub fn with_data_and_options(data: D, options: Options) -> Nickel<D>
pub fn with_data_and_options(data: D, options: Options) -> Nickel<D>
Creates an instance of Nickel with default error handling, default Ooptions, and custom data.
sourcepub fn with_data(data: D) -> Nickel<D>
pub fn with_data(data: D) -> Nickel<D>
Creates an instance of Nickel with default error handling, default Ooptions, and custom data.
sourcepub fn utilize<T: Middleware<D>>(&mut self, handler: T)
pub 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);
});
sourcepub fn handle_error<T: ErrorHandler<D>>(&mut self, handler: T)
pub 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)
sourcepub fn router() -> Router<D>
pub 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);
}
sourcepub fn listen<T: ToSocketAddrs>(
self,
addr: T
) -> Result<ListeningServer, Box<dyn StdError>>
pub fn listen<T: ToSocketAddrs>(
self,
addr: T
) -> Result<ListeningServer, Box<dyn 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());
sourcepub fn keep_alive_timeout(&mut self, timeout: Option<Duration>)
pub 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.
sourcepub fn listen_https<T, S>(
self,
addr: T,
ssl: S
) -> Result<ListeningServer, Box<dyn StdError>>where
T: ToSocketAddrs,
S: SslServer + Send + Clone + 'static,
pub fn listen_https<T, S>(
self,
addr: T,
ssl: S
) -> Result<ListeningServer, Box<dyn 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
sourceimpl<D: Sync + Send + 'static> HttpRouter<D> for Nickel<D>
impl<D: Sync + Send + 'static> HttpRouter<D> for Nickel<D>
sourcefn add_route<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
method: Method,
matcher: M,
handler: H
) -> &mut Self
fn add_route<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
method: Method,
matcher: M,
handler: H
) -> &mut Self
RequestHandler
trait. Read moresourcefn get<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
fn get<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
sourcefn post<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
fn post<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
sourcefn put<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
fn put<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
sourcefn delete<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
fn delete<M: Into<Matcher>, H: Middleware<D>>(
&mut self,
matcher: M,
handler: H
) -> &mut Self
sourceimpl<D> Mountable<D> for Nickel<D>where
D: Send + Sync + 'static,
impl<D> Mountable<D> for Nickel<D>where
D: Send + Sync + 'static,
sourcefn mount<S: Into<String>, M: Middleware<D>>(
&mut self,
mount_point: S,
middleware: M
)
fn mount<S: Into<String>, M: Middleware<D>>(
&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.