Struct dev_bestia_simple_server::Server [−][src]
pub struct Server { /* fields omitted */ }
Expand description
A web server.
This is the core type of this crate, and is used to create a new server and listen for connections.
Implementations
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
use cargo_crev_reviews::Server;
fn main() {
let server = Server::new(|request, mut response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
}
pub fn with_timeout<H>(timeout: Duration, handler: H) -> Server where
H: Fn(Request<Vec<u8>>, ResponseBuilder) -> ResponseResult + 'static + Send + Sync,
pub fn with_timeout<H>(timeout: Duration, handler: H) -> Server where
H: Fn(Request<Vec<u8>>, ResponseBuilder) -> ResponseResult + 'static + Send + Sync,
Constructs a new server with the given handler and the specified request timeout.
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
use std::time::Duration;
use cargo_crev_reviews::Server;
fn main() {
let server = Server::with_timeout(Duration::from_secs(5), |request, response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
}
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. By default, that
directory is “public” in the same directory as where it’s run. If you’d like to change
this default, please see the set_static_directory
method.
If someone tries a path directory traversal attack, this will return a
404
. Please note that this is a best effort at the
moment.
Panics
There are several circumstances in which listen
can currently panic:
- If there’s an error constructing a TcpListener, generally if the port
or host is incorrect. See
TcpListener
’s docs for more. - If the connection fails, see
incoming
’s docs for more.
Finally, if reading from the stream fails. Timeouts and connection closes are handled, other errors may result in a panic. This will only take down one of the threads in the threadpool, rather than the whole server.
Examples
use cargo_crev_reviews::Server;
fn main() {
let server = Server::new(|request, mut response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
server.listen("127.0.0.1", "7979");
}
Tells the server to listen on a provided TcpListener
.
A threadpool is created, and used to handle connections. The pool size is four threads.
This method blocks forever.
This 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
use cargo_crev_reviews::Server;
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind(("127.0.0.1", 7979))
.expect("Error starting the server.");
let server = Server::new(|request, mut response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
server.listen_on_socket(listener);
}
Sets the proper directory for serving static files.
By default, the server will serve static files inside a public
directory. This method lets you set a path to whatever location
you’d like.
Examples
use cargo_crev_reviews::Server;
fn main() {
let mut server = Server::new(|request, mut response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
server.set_static_directory("/var/www/");
server.listen("127.0.0.1", "7979");
}
Disables serving static files.
By default, the server will serve static files inside a public
directory, or the directory set by set_static_directory
. This
method lets you disable this.
It can be re-enabled by a subsequent call to set_static_directory
.
Examples
use cargo_crev_reviews::Server;
fn main() {
let mut server = Server::new(|request, mut response| {
Ok(response.body("Hello, world!".as_bytes().to_vec())?)
});
server.dont_serve_static_files();
server.listen("127.0.0.1", "7979");
}