Struct Server

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

The core of Tusk, Server is a async/await ready web server.

Server is generic over V, a configuration or state type of your choosing. A clone of this value is injected into each Request allowing handlers to share application state without resorting to global variables. Because the type is determined at compile time there is no dynamic dispatch involved.

Implementations§

Source§

impl<V: 'static> Server<V>

Source

pub async fn new( port: i32, database: DatabaseConfig, configuration: V, ) -> Server<V>

Create a new server listening on the specified port and backed by the provided DatabaseConfig.

configuration is any user provided state that will be injected into every Request handled by this server.

Source

pub fn enable_debugging(&mut self)

Enable debugging. This will enable printing verbose information. This is useful for debugging queries and other issues.

Source

pub fn disable_debugging(&mut self)

Disable debugging. This will disable printing verbose information. This is the default state.

Source

pub fn register<H, Fut>(&mut self, method: HttpMethod, path: &str, f: H)
where H: Fn(Request<V>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Response, RouteError>> + 'static,

Register a single [Route]. Routes should NOT be registered after calling Server::start, as all routes are sorted for peformance when start is called.

See Server::register for a better way to register routes.

Source

pub fn module<T: RouteModule<V>>(&mut self, prefix: &str, module: T)

Register many [Route]s at once. Routes should NOT be registered after calling Server::start, as all routes are sorted for peformance when start is called.

The recommended pattern for this is to break out related routes into their own module and decorate each route with #[route], then export a type implementing RouteModule for that collection. Because the trait is generic over V it retains access to the same configuration type that the server uses. All generics are monomorphized so this organizational layer has zero runtime penalty.

Source

pub fn set_postfix(&mut self, f: fn(Response) -> Response)

Add function that can modify all outgoing responses. Useful for setting headers.

Source

pub fn set_cors(&mut self, origin: &str, headers: &str)

Configure the CORS headers that will be applied to every response.

Source

pub async fn start(self)

Prepare all registered routes and begin listening for connections.

Source

pub fn handle_options(&self) -> Response

Generate a minimal response for HTTP OPTIONS requests.

Auto Trait Implementations§

§

impl<V> !Freeze for Server<V>

§

impl<V> !RefUnwindSafe for Server<V>

§

impl<V> Send for Server<V>
where V: Sync + Send,

§

impl<V> Sync for Server<V>
where V: Sync + Send,

§

impl<V> Unpin for Server<V>

§

impl<V> !UnwindSafe for Server<V>

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V