pub struct Server<T, V> { /* private fields */ }
Expand description
The core of Tusk, Server
is a async/await ready
web server.
Server accepts a generic type T
. This type is injected
into all routes as the final argument.
Implementations§
source§impl<T: 'static, V: 'static> Server<T, V>
impl<T: 'static, V: 'static> Server<T, V>
sourcepub async fn new(
port: i32,
database: DatabaseConfig,
treatment: Box<fn(_: Request, _: DatabaseConnection, _: Rc<V>) -> Pin<Box<dyn Future<Output = Result<(T, Request, DatabaseConnection), RouteError>>>>>,
initialization_data: V
) -> Server<T, V>
pub async fn new( port: i32, database: DatabaseConfig, treatment: Box<fn(_: Request, _: DatabaseConnection, _: Rc<V>) -> Pin<Box<dyn Future<Output = Result<(T, Request, DatabaseConnection), RouteError>>>>>, initialization_data: V ) -> Server<T, V>
Create a new server.
Specify a port, DatabaseConfig
, and an async
function with arguments Request
and a PostgresConn
(alias for [Object
]) and returns T
.
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(&mut self, r: Route<T>)
pub fn register(&mut self, r: Route<T>)
Register a 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(&mut self, prefix: &str, rs: Vec<Route<T>>)
pub fn module(&mut self, prefix: &str, rs: Vec<Route<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 module function which returns a Vec of all the routes within. Note that this has no effect on performance, this just keeps your code organized.
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 requests. Useful for setting headers.
sourcepub async fn start(&mut self)
pub async fn start(&mut self)
Prepares Tusk for serving applications and then begins listening.