Struct oxhttp::Server

source ·
pub struct Server { /* private fields */ }
Expand description

An HTTP server.

It currently provides two threading approaches:

  • If the rayon feature is enabled a thread pool is used. The number of threads can be set with the Server::set_num_threads feature. By default 4 times the number of available logical cores is used.
  • If the rayon feature is not enabled, a new thread is started on each connection and kept while the client connection is not closed. Use it at our own risks!
use oxhttp::Server;
use oxhttp::model::{Response, Status};
use std::time::Duration;

// Builds a new server that returns a 404 everywhere except for "/" where it returns the body 'home'
let mut server = Server::new(|request| {
    if request.url().path() == "/" {
        Response::builder(Status::OK).with_body("home")
    } else {
        Response::builder(Status::NOT_FOUND).build()
    }
});
// Raise a timeout error if the client does not respond after 10s.
server.set_global_timeout(Duration::from_secs(10));
// Listen to localhost:8080
server.listen(("localhost", 8080))?;

Implementations§

source§

impl Server

source

pub fn new( on_request: impl Fn(&mut Request) -> Response + Send + Sync + 'static ) -> Self

Builds the server using the given on_request method that builds a Response from a given Request.

source

pub fn set_server_name( &mut self, server: impl Into<String> ) -> Result<(), InvalidHeader>

Sets the default value for the Server header.

source

pub fn set_global_timeout(&mut self, timeout: Duration)

Sets the global timeout value (applies to both read and write).

source

pub fn set_num_threads(&mut self, num_threads: usize)

Sets that the server should use a thread pool with this number of threads.

source

pub fn listen(&self, address: impl ToSocketAddrs) -> Result<()>

Runs the server by listening to address.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl !UnwindSafe for Server

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.