use std::sync::Arc;
use crate::{
config::Config,
http::router::Router,
middleware::{trace_requests, MiddlewareHandler},
panic_hook::panic_hook,
Server,
};
pub trait App {
fn with_routes() -> Router {
Router::builder().build().unwrap()
}
fn with_config() -> Config {
Config::try_from_fs().expect("failed to retrieve config from file `snx.toml`")
}
fn with_global_middleware() -> Vec<MiddlewareHandler> {
vec![Arc::new(Box::new(trace_requests))]
}
fn with_tracing() {
tracing_subscriber::fmt().with_target(false).init();
}
}
pub fn boot<A: App>() {
let router = A::with_routes();
let config = A::with_config();
let global_middleware = A::with_global_middleware();
A::with_tracing();
std::panic::set_hook(Box::new(panic_hook));
let addr = format!("{}:{}", config.server.host, config.server.port);
Server::try_bind(addr, router, global_middleware)
.unwrap()
.num_threads(config.server.num_threads)
.serve();
}