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.
Configuration options for the server.
Creates an instance of Nickel with default error handling.
Creates and instance of Nickel with custom Options.
Creates an instance of Nickel with default error handling,
default Ooptions, and custom data.
Creates an instance of Nickel with default error handling,
default Ooptions, 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.
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.
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.
#[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.
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
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.
The default value is 75 seconds.
Bind and listen for connections on the given host and port.
Only accepts SSL connections
Panics if addr
is an invalid SocketAddr
.
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();
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
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
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
A trait that makes mounting more convenient. Works the same as
manually adding a Mount
middleware.
use nickel::{Nickel, StaticFilesHandler, Mountable};
let mut server = Nickel::new();
server.mount("/static_files/", StaticFilesHandler::new("/path/to/serve/"));
Panics if mount_point does not have a leading and trailing slash.
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
impl<T> Typeable for T where T: Any, | |
Get the TypeId
of this object.
impl<T> UnsafeAny for T where T: Any, | |