Struct AIOServer

Source
pub struct AIOServer<H> { /* private fields */ }
Expand description

Main struct of the crate, represent the http server

Implementations§

Source§

impl<H> AIOServer<H>
where H: Send + Sync + 'static + Fn(&Request) -> Response,

Source

pub fn new(size: i32, addr: &str, handler: H) -> AIOServer<H>

Start the server with the given thread pool size and bind to the given address The given function is executed for each http request received

§Argument
  • size - Number of thread that will be spawned minimum is 1. The total minimum number of thread is 2 : 1 to handle request and 1 to run the event loop
  • addr - Address the server will bind to. The format is the same as std::net::TcpListener. If the address is incorrect or cannot be bound to, the function will panic
  • handler - function executed for each received http request
§Example

Create a simple server that will respond with a HTTP response with status 200, content type header “text/plain” and body “Hello”

let server = mini_async_http::AIOServer::new(3, "0.0.0.0:7878", move |request|{
    mini_async_http::ResponseBuilder::empty_200()
        .body(b"Hello")
        .content_type("text/plain")
        .build()
        .unwrap()
});
Source

pub fn start(&mut self)

Start the event loop. This call is blocking but you can still interact with the server through the Handle

§Example

Create a simple server and then start it. It is started from another thread as the start call is blocking. After spawning the thread, wait for the server to be ready and then shut it down

let mut server = mini_async_http::AIOServer::new(3, "0.0.0.0:7879", move |request|{
    mini_async_http::ResponseBuilder::empty_200()
        .body(b"Hello")
        .content_type("text/plain")
        .build()
        .unwrap()
});
let handle = server.handle();

std::thread::spawn(move || {
    server.start();
});

handle.ready();
handle.shutdown();
Source§

impl<H> AIOServer<H>

Source

pub fn handle(&self) -> ServerHandle

Get a ServerHandle to this server

Trait Implementations§

Source§

impl<H> Drop for AIOServer<H>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<H> !Freeze for AIOServer<H>

§

impl<H> !RefUnwindSafe for AIOServer<H>

§

impl<H> Send for AIOServer<H>
where H: Sync + Send,

§

impl<H> !Sync for AIOServer<H>

§

impl<H> Unpin for AIOServer<H>

§

impl<H> !UnwindSafe for AIOServer<H>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.