1use crate::Router;
2
3pub use inventory::submit;
4
5pub trait TypedHandlerRegistrar: Send + Sync + 'static {
7 fn install_route(&self, router: Router) -> Router;
9}
10
11pub trait TypeRouter {
13 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#[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
45pub 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}