Skip to main content

framework_web/
route.rs

1use crate::{AuthRule, WebMethod, WebResponse};
2use futures_util::future::BoxFuture;
3use std::sync::Arc;
4
5pub type WebRouteFuture = BoxFuture<'static, WebResponse>;
6pub type WebRouteInvoke = Arc<dyn Fn() -> WebRouteFuture + Send + Sync>;
7
8pub struct WebRoute {
9    pub method: WebMethod,
10    pub path: String,
11    pub auth: AuthRule,
12    pub invoke: WebRouteInvoke,
13}
14
15#[cfg(feature = "collect")]
16#[doc(hidden)]
17pub struct WebApiBuilder(pub fn() -> WebRoute);
18
19#[cfg(feature = "collect")]
20inventory::collect!(WebApiBuilder);
21
22impl WebRoute {
23    pub async fn invoke(&self) -> WebResponse {
24        (self.invoke)().await
25    }
26}
27
28#[cfg(feature = "collect")]
29pub fn web_api_iter() -> Box<dyn Iterator<Item = WebRoute>> {
30    Box::new(
31        inventory::iter::<WebApiBuilder>
32            .into_iter()
33            .map(|builder| (builder.0)()),
34    )
35}