use crate::builder::AppBuilder;
use crate::plugin::Plugin;
use tower_http::cors::CorsLayer;
pub struct Cors {
layer: CorsLayer,
}
impl Cors {
pub fn permissive() -> Self {
Self {
layer: crate::layers::default_cors(),
}
}
pub fn custom(layer: CorsLayer) -> Self {
Self { layer }
}
}
impl<T: Clone + Send + Sync + 'static> Plugin<T> for Cors {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
app.with_layer_fn(move |router| router.layer(self.layer))
}
}
pub struct Tracing;
impl<T: Clone + Send + Sync + 'static> Plugin<T> for Tracing {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
app.with_layer_fn(|router| router.layer(crate::layers::default_trace()))
}
}
pub struct Health;
impl<T: Clone + Send + Sync + 'static> Plugin<T> for Health {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
app.register_routes(
crate::http::Router::new()
.route("/health", crate::http::routing::get(health_handler)),
)
}
}
async fn health_handler() -> &'static str {
"OK"
}
pub struct ErrorHandling;
impl<T: Clone + Send + Sync + 'static> Plugin<T> for ErrorHandling {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
app.with_layer_fn(|router| router.layer(crate::layers::catch_panic_layer()))
}
}
pub struct DevReload;
impl<T: Clone + Send + Sync + 'static> Plugin<T> for DevReload {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
app.register_routes(crate::dev::dev_routes())
}
}
pub struct NormalizePath;
impl<T: Clone + Send + Sync + 'static> Plugin<T> for NormalizePath {
fn install(self, app: AppBuilder<T>) -> AppBuilder<T> {
use tower_http::normalize_path::NormalizePathLayer;
app.with_layer_fn(|router| router.layer(NormalizePathLayer::trim_trailing_slash()))
}
}