1
use std::collections::HashMap;
2

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

            
5
use sylvia_iot_corelib::role::Role;
6

            
7
use super::super::{
8
    State,
9
    middleware::{AuthService, RoleScopeType},
10
};
11

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

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

            
24
6682
    match state.api_scopes.get("unit.post") {
25
6680
        None => {
26
6680
            role_scopes_root.insert(Method::POST, (vec![], vec![]));
27
6680
        }
28
2
        Some(scopes) => {
29
2
            role_scopes_root.insert(Method::POST, (vec![], scopes.clone()));
30
2
        }
31
    }
32
6682
    match state.api_scopes.get("unit.get") {
33
6680
        None => {
34
6680
            role_scopes_count.insert(Method::GET, (vec![], vec![]));
35
6680
            role_scopes_list.insert(Method::GET, (vec![], vec![]));
36
6680
            role_scopes_param.insert(Method::GET, (vec![], vec![]));
37
6680
        }
38
2
        Some(scopes) => {
39
2
            role_scopes_count.insert(Method::GET, (vec![], scopes.clone()));
40
2
            role_scopes_list.insert(Method::GET, (vec![], scopes.clone()));
41
2
            role_scopes_param.insert(Method::GET, (vec![], scopes.clone()));
42
2
        }
43
    }
44
6682
    match state.api_scopes.get("unit.patch") {
45
6680
        None => {
46
6680
            role_scopes_param.insert(Method::PATCH, (vec![], vec![]));
47
6680
        }
48
2
        Some(scopes) => {
49
2
            role_scopes_param.insert(Method::PATCH, (vec![], scopes.clone()));
50
2
        }
51
    }
52
6682
    match state.api_scopes.get("unit.delete") {
53
6680
        None => {
54
6680
            role_scopes_param.insert(Method::DELETE, (vec![], vec![]));
55
6680
        }
56
2
        Some(scopes) => {
57
2
            role_scopes_param.insert(Method::DELETE, (vec![], scopes.clone()));
58
2
        }
59
    }
60
6682
    match state.api_scopes.get("unit.delete.user") {
61
6680
        None => {
62
6680
            role_scopes_user.insert(Method::DELETE, (vec![Role::ADMIN, Role::MANAGER], vec![]));
63
6680
        }
64
2
        Some(scopes) => {
65
2
            role_scopes_user.insert(
66
2
                Method::DELETE,
67
2
                (vec![Role::ADMIN, Role::MANAGER], scopes.clone()),
68
2
            );
69
2
        }
70
    }
71

            
72
6682
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
73
6682
    Router::new().nest(
74
6682
        scope_path,
75
6682
        Router::new()
76
6682
            .route(
77
6682
                "/",
78
6682
                routing::post(api::post_unit)
79
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_root)),
80
6682
            )
81
6682
            .route(
82
6682
                "/count",
83
6682
                routing::get(api::get_unit_count)
84
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_count)),
85
6682
            )
86
6682
            .route(
87
6682
                "/list",
88
6682
                routing::get(api::get_unit_list)
89
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_list)),
90
6682
            )
91
6682
            .route(
92
6682
                "/{unit_id}",
93
6682
                routing::get(api::get_unit)
94
6682
                    .patch(api::patch_unit)
95
6682
                    .delete(api::delete_unit)
96
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_param)),
97
6682
            )
98
6682
            .route(
99
6682
                "/user/{user_id}",
100
6682
                routing::delete(api::delete_unit_user)
101
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_user)),
102
6682
            )
103
6682
            .with_state(state.clone()),
104
6682
    )
105
6682
}