1
use std::collections::HashMap;
2

            
3
use axum::{Router, http::Method, routing};
4

            
5
use super::super::{
6
    State,
7
    middleware::{AuthService, RoleScopeType},
8
};
9

            
10
mod api;
11
mod request;
12
mod response;
13
pub use api::{init, new_ctrl_receiver, new_ctrl_sender};
14

            
15
6682
pub fn new_service(scope_path: &str, state: &State) -> Router {
16
6682
    let mut role_scopes_root: HashMap<Method, RoleScopeType> = HashMap::new();
17
6682
    let mut role_scopes_count: HashMap<Method, RoleScopeType> = HashMap::new();
18
6682
    let mut role_scopes_list: HashMap<Method, RoleScopeType> = HashMap::new();
19
6682
    let mut role_scopes_param: HashMap<Method, RoleScopeType> = HashMap::new();
20

            
21
6682
    match state.api_scopes.get("application.post") {
22
6680
        None => {
23
6680
            role_scopes_root.insert(Method::POST, (vec![], vec![]));
24
6680
        }
25
2
        Some(scopes) => {
26
2
            role_scopes_root.insert(Method::POST, (vec![], scopes.clone()));
27
2
        }
28
    }
29
6682
    match state.api_scopes.get("application.get") {
30
6680
        None => {
31
6680
            role_scopes_count.insert(Method::GET, (vec![], vec![]));
32
6680
            role_scopes_list.insert(Method::GET, (vec![], vec![]));
33
6680
            role_scopes_param.insert(Method::GET, (vec![], vec![]));
34
6680
        }
35
2
        Some(scopes) => {
36
2
            role_scopes_count.insert(Method::GET, (vec![], scopes.clone()));
37
2
            role_scopes_list.insert(Method::GET, (vec![], scopes.clone()));
38
2
            role_scopes_param.insert(Method::GET, (vec![], scopes.clone()));
39
2
        }
40
    }
41
6682
    match state.api_scopes.get("application.patch") {
42
6680
        None => {
43
6680
            role_scopes_param.insert(Method::PATCH, (vec![], vec![]));
44
6680
        }
45
2
        Some(scopes) => {
46
2
            role_scopes_param.insert(Method::PATCH, (vec![], scopes.clone()));
47
2
        }
48
    }
49
6682
    match state.api_scopes.get("application.delete") {
50
6680
        None => {
51
6680
            role_scopes_param.insert(Method::DELETE, (vec![], vec![]));
52
6680
        }
53
2
        Some(scopes) => {
54
2
            role_scopes_param.insert(Method::DELETE, (vec![], scopes.clone()));
55
2
        }
56
    }
57

            
58
6682
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
59
6682
    Router::new().nest(
60
6682
        scope_path,
61
6682
        Router::new()
62
6682
            .route(
63
6682
                "/",
64
6682
                routing::post(api::post_application).layer(AuthService::new(
65
6682
                    state.client.clone(),
66
6682
                    auth_uri.clone(),
67
6682
                    role_scopes_root,
68
                )),
69
            )
70
6682
            .route(
71
6682
                "/count",
72
6682
                routing::get(api::get_application_count).layer(AuthService::new(
73
6682
                    state.client.clone(),
74
6682
                    auth_uri.clone(),
75
6682
                    role_scopes_count,
76
                )),
77
            )
78
6682
            .route(
79
6682
                "/list",
80
6682
                routing::get(api::get_application_list).layer(AuthService::new(
81
6682
                    state.client.clone(),
82
6682
                    auth_uri.clone(),
83
6682
                    role_scopes_list,
84
                )),
85
            )
86
6682
            .route(
87
6682
                "/{application_id}",
88
6682
                routing::get(api::get_application)
89
6682
                    .patch(api::patch_application)
90
6682
                    .delete(api::delete_application)
91
6682
                    .layer(AuthService::new(
92
6682
                        state.client.clone(),
93
6682
                        auth_uri.clone(),
94
6682
                        role_scopes_param,
95
                    )),
96
            )
97
6682
            .with_state(state.clone()),
98
    )
99
6682
}