Expand description
A HTTP server
Architecture
The server creates a TcpListener bound to the given address. Then it creates a FIFO-queue to handle incoming
connections, and spawns an acceptor-thread that accepts all incoming connections and sends them to the FIFO-queue.
The exec-function loops forever over the queued connections and sends them together with the callback into a
thread-pool to process them. If a connection is kept-alive, it is requeued into the FIFO-queue after the response has
been sent to the client to be reprocessed again.
accept--->connection-queue--->threadpool
^ |
| |
keep-alive |
^ |
| v
close<----------------------thread-worker
Implementations§
source§impl<T, const STACK_SIZE: usize> Server<T, STACK_SIZE>
impl<T, const STACK_SIZE: usize> Server<T, STACK_SIZE>
sourcepub fn new(
address: &str,
soft_limit: usize,
hard_limit: usize
) -> Result<Self, Error>
pub fn new(
address: &str,
soft_limit: usize,
hard_limit: usize
) -> Result<Self, Error>
Creates a new server bound on the given address
Examples found in repository?
examples/helloworld.rs (line 24)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |_: &mut Request| {
let mut response = Response::new_200_ok();
response.set_body_static(b"Hello world\r\n");
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}More examples
examples/helloworld-nokeepalive.rs (line 25)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |_: &mut Request| {
let mut response = Response::new_200_ok();
response.set_body_static(b"Hello world\r\n");
response.set_connection_close();
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}examples/teapot.rs (line 30)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |request: &mut Request| {
// Create the response body
let mut message = b"There are only teapots in ".to_vec();
message.extend_from_slice(&request.target);
message.extend_from_slice(b"\r\n");
// Send the response
let mut response = Response::new_status_reason(418, "I'm a teapot");
response.set_body_data(message);
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}sourcepub fn exec<F>(self, callback: F) -> Result<Infallible, Error>where
F: Fn(&mut Request) -> Response<T> + Send + Sync + 'static,
T: Read + 'static,
pub fn exec<F>(self, callback: F) -> Result<Infallible, Error>where
F: Fn(&mut Request) -> Response<T> + Send + Sync + 'static,
T: Read + 'static,
Starts the server
Examples found in repository?
examples/helloworld.rs (line 25)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |_: &mut Request| {
let mut response = Response::new_200_ok();
response.set_body_static(b"Hello world\r\n");
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}More examples
examples/helloworld-nokeepalive.rs (line 26)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |_: &mut Request| {
let mut response = Response::new_200_ok();
response.set_body_static(b"Hello world\r\n");
response.set_connection_close();
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}examples/teapot.rs (line 31)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
// Log everything
log::set_level(WARN);
// Define our request handler
let request_handler = |request: &mut Request| {
// Create the response body
let mut message = b"There are only teapots in ".to_vec();
message.extend_from_slice(&request.target);
message.extend_from_slice(b"\r\n");
// Send the response
let mut response = Response::new_status_reason(418, "I'm a teapot");
response.set_body_data(message);
response
};
// Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
// temporary worker threads under high load
let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
server.exec(request_handler).expect("server failed");
}