use topcoat_core::context::CxBuilder;
use topcoat_router::{Body, Layer, LayerFuture, Next, Path, RouterBuilder};
use crate::{Config, OriginLayer, SessionState};
#[derive(Debug, Clone, Copy, Default)]
pub struct SessionLayer;
impl SessionLayer {
#[must_use]
pub const fn new() -> Self {
Self
}
}
impl Layer for SessionLayer {
fn path(&self) -> &Path {
Path::new("/")
}
fn handle<'a>(&'a self, cx: &'a mut CxBuilder, body: Body, next: Next<'a>) -> LayerFuture<'a> {
cx.insert(SessionState::new());
next.run(cx, body)
}
}
pub trait RouterBuilderSessionExt {
#[must_use]
fn sessions(self, config: Config) -> Self;
}
impl RouterBuilderSessionExt for RouterBuilder {
fn sessions(mut self, config: Config) -> Self {
let verify_origin = config.verify_origin;
self = self.app_context(config);
self = self.layer(SessionLayer::new());
if verify_origin {
self = self.layer(OriginLayer::new());
}
self
}
}