spring_web/
handler.rs

1use crate::Router;
2
3pub use inventory::submit;
4
5/// TypeHandler is used to configure the spring-macro marked route handler
6pub trait TypedHandlerRegistrar: Send + Sync + 'static {
7    /// install route
8    fn install_route(&self, router: Router) -> Router;
9}
10
11/// Add typed routes marked with procedural macros
12pub trait TypeRouter {
13    /// Add typed routes marked with procedural macros
14    fn typed_route<F: TypedHandlerRegistrar>(self, factory: F) -> Self;
15}
16
17impl TypeRouter for Router {
18    fn typed_route<F: TypedHandlerRegistrar>(self, factory: F) -> Self {
19        factory.install_route(self)
20    }
21}
22
23inventory::collect!(&'static dyn TypedHandlerRegistrar);
24
25/// auto_config
26#[macro_export]
27macro_rules! submit_typed_handler {
28    ($ty:ident) => {
29        ::spring_web::handler::submit! {
30            &$ty as &dyn ::spring_web::handler::TypedHandlerRegistrar
31        }
32    };
33}
34
35#[cfg(feature = "socket_io")]
36#[macro_export]
37macro_rules! submit_socketio_handler {
38    ($ty:ident) => {
39        ::spring_web::handler::submit! {
40            &$ty as &dyn ::spring_web::handler::SocketIOHandlerRegistrar
41        }
42    };
43}
44
45/// auto_config
46pub fn auto_router() -> Router {
47    #[cfg(feature = "openapi")]
48    crate::enable_openapi();
49
50    let mut router = Router::new();
51    for handler in inventory::iter::<&dyn TypedHandlerRegistrar> {
52        router = handler.install_route(router);
53    }
54    router
55}
56
57#[cfg(feature = "socket_io")]
58pub trait SocketIOHandlerRegistrar: Send + Sync + 'static {
59    fn install_socketio_handlers(&self, socket: &crate::socketioxide::extract::SocketRef);
60}
61
62#[cfg(feature = "socket_io")]
63inventory::collect!(&'static dyn SocketIOHandlerRegistrar);
64
65#[cfg(feature = "socket_io")]
66pub fn auto_socketio_setup(socket: &crate::socketioxide::extract::SocketRef) {
67    for handler in inventory::iter::<&dyn SocketIOHandlerRegistrar> {
68        handler.install_socketio_handlers(socket);
69    }
70}