Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Expand description

Low-level multi-threaded server.

Use this when you want full control over the Router (e.g. adding middleware, merging sub-routers) instead of the macro-driven Chopin builder.

§Example

use chopin_core::{Router, Server, Context, Response};

fn ping(_ctx: Context) -> Response { Response::text("pong") }

let mut router = Router::new();
router.get("/ping", ping);

Server::bind("0.0.0.0:8080")
    .workers(4)
    .serve(router)
    .unwrap();

Implementations§

Source§

impl Server

Source

pub fn bind(host_port: &str) -> Self

Bind to the given address. Defaults to one worker per logical CPU.

Source

pub fn workers(self, workers: usize) -> Self

Set the number of worker threads (defaults to num_cpus::get()).

Source

pub fn serve(self, router: Router) -> ChopinResult<()>

Start the server with the provided router. Spawns one thread per worker, each pinned to a CPU core, and blocks until shutdown.

Auto Trait Implementations§

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.