1
use std::collections::HashMap;
2

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

            
5
use sylvia_iot_corelib::role::Role;
6

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

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

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

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

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