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>
impl<V: 'static> Server<V>
Sourcepub async fn new(
port: i32,
database: DatabaseConfig,
configuration: V,
) -> Server<V>
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.
Sourcepub fn enable_debugging(&mut self)
pub fn enable_debugging(&mut self)
Enable debugging. This will enable printing verbose information. This is useful for debugging queries and other issues.
Sourcepub fn disable_debugging(&mut self)
pub fn disable_debugging(&mut self)
Disable debugging. This will disable printing verbose information. This is the default state.
Sourcepub fn register<H, Fut>(&mut self, method: HttpMethod, path: &str, f: H)
pub fn register<H, Fut>(&mut self, method: HttpMethod, path: &str, f: H)
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.
Sourcepub fn module<T: RouteModule<V>>(&mut self, prefix: &str, module: T)
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.
Sourcepub fn set_postfix(&mut self, f: fn(Response) -> Response)
pub fn set_postfix(&mut self, f: fn(Response) -> Response)
Add function that can modify all outgoing responses. Useful for setting headers.
Sourcepub fn set_cors(&mut self, origin: &str, headers: &str)
pub fn set_cors(&mut self, origin: &str, headers: &str)
Configure the CORS headers that will be applied to every response.
Sourcepub fn handle_options(&self) -> Response
pub fn handle_options(&self) -> Response
Generate a minimal response for HTTP OPTIONS
requests.