Struct simple_server::Server
[−]
[src]
pub struct Server<T> { /* fields omitted */ }
A web server.
This is the core type of this crate, and is used to create a new server and listen for connections.
Methods
impl<'a, T: Into<Cow<'a, [u8]>>> Server<T>
[src]
fn new(
handler: fn(_: Request<&[u8]>, _: ResponseBuilder) -> Result<Response<T>, Error>
) -> Self
[src]
handler: fn(_: Request<&[u8]>, _: ResponseBuilder) -> Result<Response<T>, Error>
) -> Self
Constructs a new server with the given handler.
The handler function is called on all requests.
Errors
The handler function returns a Result
so that you may use ?
to
handle errors. If a handler returns an Err
, a 500 will be shown.
If you'd like behavior other than that, return an Ok(Response)
with
the proper error code. In other words, this behavior is to gracefully
handle errors you don't care about, not for properly handling
non-HTTP 200
responses.
Examples
extern crate simple_server; use simple_server::Server; fn main() { let server = Server::new(|request, mut response| { Ok(response.body("Hello, world!".as_bytes())?) }); }
fn listen(&self, host: &str, port: &str)
[src]
Tells the server to listen on a specified host and port.
A threadpool is created, and used to handle connections. The pool size is four threads.
This method blocks forever.
The listen
method will also serve static files out of a public
directory in the same directory as where it's run. If someone tries
a path directory traversal attack, this will return a 404
.
Examples
extern crate simple_server; use simple_server::Server; fn main() { let server = Server::new(|request, mut response| { Ok(response.body("Hello, world!".as_bytes())?) }); server.listen("127.0.0.1", "7979"); }