Skip to main content

shield_axum/
router.rs

1#[cfg(feature = "utoipa")]
2use axum::routing::post;
3use axum::{
4    Router,
5    routing::{any, get},
6};
7use shield::{Shield, User};
8#[cfg(feature = "utoipa")]
9use utoipa::OpenApi;
10#[cfg(feature = "utoipa")]
11use utoipa_axum::router::OpenApiRouter;
12
13use crate::routes::*;
14
15#[cfg(feature = "utoipa")]
16#[cfg_attr(feature = "utoipa", derive(utoipa::OpenApi))]
17#[cfg_attr(feature = "utoipa", openapi(paths(action, forms, user)))]
18struct BaseOpenApi;
19
20pub struct AuthRoutes<U: User> {
21    #[cfg_attr(not(feature = "utoipa"), expect(dead_code))]
22    shield: Shield<U>,
23}
24
25impl<U: Clone + User + 'static> AuthRoutes<U> {
26    pub fn new(shield: Shield<U>) -> Self {
27        Self { shield }
28    }
29
30    pub fn router<S: Clone + Send + Sync + 'static>(&self) -> Router<S> {
31        Router::new()
32            .route("/user", get(user::<U>))
33            .route("/forms/{actionId}", get(forms::<U>))
34            .route("/{methodId}/{actionId}", any(action::<U>))
35            .route("/{methodId}/{actionId}/{providerId}", any(action::<U>))
36    }
37
38    #[cfg(feature = "utoipa")]
39    pub fn openapi_router<S: Clone + Send + Sync + 'static>(&self) -> OpenApiRouter<S> {
40        OpenApiRouter::with_openapi(BaseOpenApi::openapi().merge_from(self.shield.openapi()))
41            .route("/user", get(user::<U>))
42            .route("/forms/{actionId}", get(forms::<U>))
43            .route("/{methodId}/{actionId}", get(action::<U>))
44            .route("/{methodId}/{actionId}", post(action::<U>))
45            .route("/{methodId}/{actionId}/{providerId}", get(action::<U>))
46            .route("/{methodId}/{actionId}/{providerId}", post(action::<U>))
47    }
48}