pub struct Server<const STACK_SIZE: usize> { /* private fields */ }Expand description
A threadpool-based HTTP server
Implementations§
Source§impl<const STACK_SIZE: usize> Server<STACK_SIZE>
impl<const STACK_SIZE: usize> Server<STACK_SIZE>
Sourcepub fn with_source_sink<F>(workers_max: usize, source_sink_handler: F) -> Self
pub fn with_source_sink<F>(workers_max: usize, source_sink_handler: F) -> Self
Creates a new server with the given connection handler
Sourcepub fn with_request_response<F>(
workers_max: usize,
request_response_handler: F,
) -> Self
pub fn with_request_response<F>( workers_max: usize, request_response_handler: F, ) -> Self
Creates a new server with the given connection handler
Examples found in repository?
examples/helloworld.rs (lines 7-11)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |_| {
8 let mut response = Response::new_200_ok();
9 response.set_body_data(b"Hello world\r\n");
10 response
11 });
12
13 // Handle incoming connections
14 server.accept("[::]:9999").expect("server failed");
15}More examples
examples/helloworld-nokeepalive.rs (lines 7-12)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |_| {
8 let mut response = Response::new_200_ok();
9 response.set_body_data(b"Hello world\r\n");
10 response.set_connection_close();
11 response
12 });
13
14 // Handle incoming connections
15 server.accept("[::]:9999").expect("server failed");
16}examples/teapot.rs (lines 7-17)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |request| {
8 // Create the response body
9 let mut message = b"There are only teapots in ".to_vec();
10 message.extend_from_slice(&request.target);
11 message.extend_from_slice(b"\r\n");
12
13 // Send the response
14 let mut response = Response::new_status_reason(418, "I'm a teapot");
15 response.set_body_data(message);
16 response
17 });
18
19 // Handle incoming connections
20 server.accept("[::]:9999").expect("server failed");
21}Sourcepub fn dispatch(&self, source: Source, sink: Sink) -> Result<(), Error>
pub fn dispatch(&self, source: Source, sink: Sink) -> Result<(), Error>
Manually dispatches a connection
Sourcepub fn accept<A>(self, address: A) -> Result<Infallible, Error>where
A: ToSocketAddrs,
pub fn accept<A>(self, address: A) -> Result<Infallible, Error>where
A: ToSocketAddrs,
Listens on the given address and accepts forever
Examples found in repository?
examples/helloworld.rs (line 14)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |_| {
8 let mut response = Response::new_200_ok();
9 response.set_body_data(b"Hello world\r\n");
10 response
11 });
12
13 // Handle incoming connections
14 server.accept("[::]:9999").expect("server failed");
15}More examples
examples/helloworld-nokeepalive.rs (line 15)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |_| {
8 let mut response = Response::new_200_ok();
9 response.set_body_data(b"Hello world\r\n");
10 response.set_connection_close();
11 response
12 });
13
14 // Handle incoming connections
15 server.accept("[::]:9999").expect("server failed");
16}examples/teapot.rs (line 20)
2fn main() {
3 use ehttpd::Server;
4 use ehttpd::http::{Response, ResponseExt};
5
6 // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7 let server = Server::with_request_response(2048, |request| {
8 // Create the response body
9 let mut message = b"There are only teapots in ".to_vec();
10 message.extend_from_slice(&request.target);
11 message.extend_from_slice(b"\r\n");
12
13 // Send the response
14 let mut response = Response::new_status_reason(418, "I'm a teapot");
15 response.set_body_data(message);
16 response
17 });
18
19 // Handle incoming connections
20 server.accept("[::]:9999").expect("server failed");
21}Auto Trait Implementations§
impl<const STACK_SIZE: usize> Freeze for Server<STACK_SIZE>
impl<const STACK_SIZE: usize> !RefUnwindSafe for Server<STACK_SIZE>
impl<const STACK_SIZE: usize> Send for Server<STACK_SIZE>
impl<const STACK_SIZE: usize> Sync for Server<STACK_SIZE>
impl<const STACK_SIZE: usize> Unpin for Server<STACK_SIZE>
impl<const STACK_SIZE: usize> !UnwindSafe for Server<STACK_SIZE>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more