topcoat_session/
router.rs1use topcoat_core::context::CxBuilder;
2use topcoat_router::{Body, Layer, LayerFuture, Next, Path, RouterBuilder};
3
4use crate::{OriginLayer, SessionConfig, SessionState};
5
6#[derive(Debug, Clone, Copy, Default)]
9pub struct SessionLayer;
10
11impl SessionLayer {
12 #[must_use]
14 pub const fn new() -> Self {
15 Self
16 }
17}
18
19impl Layer for SessionLayer {
20 fn path(&self) -> &Path {
21 Path::new("/")
22 }
23
24 fn handle<'a>(&'a self, cx: &'a mut CxBuilder, body: Body, next: Next<'a>) -> LayerFuture<'a> {
25 cx.insert(SessionState::new());
26 next.run(cx, body)
27 }
28}
29
30pub trait RouterBuilderSessionExt {
32 #[must_use]
40 fn sessions(self, config: SessionConfig) -> Self;
41}
42
43impl RouterBuilderSessionExt for RouterBuilder {
44 fn sessions(mut self, config: SessionConfig) -> Self {
45 let verify_origin = config.verify_origin;
46 self = self.app_context(config);
47 self = self.layer(SessionLayer::new());
48 if verify_origin {
49 self = self.layer(OriginLayer::new());
50 }
51 self
52 }
53}