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_bulk: HashMap<Method, RoleScopeType> = HashMap::new();
18
6682
    let mut role_scopes_bulk_del: 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

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

            
64
6682
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
65
6682
    Router::new().nest(
66
6682
        scope_path,
67
6682
        Router::new()
68
6682
            .route(
69
6682
                "/",
70
6682
                routing::post(api::post_device).layer(AuthService::new(
71
6682
                    state.client.clone(),
72
6682
                    auth_uri.clone(),
73
6682
                    role_scopes_root,
74
                )),
75
            )
76
6682
            .route(
77
6682
                "/bulk",
78
6682
                routing::post(api::post_device_bulk).layer(AuthService::new(
79
6682
                    state.client.clone(),
80
6682
                    auth_uri.clone(),
81
6682
                    role_scopes_bulk.clone(),
82
                )),
83
            )
84
6682
            .route(
85
6682
                "/bulk-delete",
86
6682
                routing::post(api::post_device_bulk_del).layer(AuthService::new(
87
6682
                    state.client.clone(),
88
6682
                    auth_uri.clone(),
89
6682
                    role_scopes_bulk_del.clone(),
90
                )),
91
            )
92
6682
            .route(
93
6682
                "/range",
94
6682
                routing::post(api::post_device_range).layer(AuthService::new(
95
6682
                    state.client.clone(),
96
6682
                    auth_uri.clone(),
97
6682
                    role_scopes_bulk,
98
                )),
99
            )
100
6682
            .route(
101
6682
                "/range-delete",
102
6682
                routing::post(api::post_device_range_del).layer(AuthService::new(
103
6682
                    state.client.clone(),
104
6682
                    auth_uri.clone(),
105
6682
                    role_scopes_bulk_del,
106
                )),
107
            )
108
6682
            .route(
109
6682
                "/count",
110
6682
                routing::get(api::get_device_count).layer(AuthService::new(
111
6682
                    state.client.clone(),
112
6682
                    auth_uri.clone(),
113
6682
                    role_scopes_count,
114
                )),
115
            )
116
6682
            .route(
117
6682
                "/list",
118
6682
                routing::get(api::get_device_list).layer(AuthService::new(
119
6682
                    state.client.clone(),
120
6682
                    auth_uri.clone(),
121
6682
                    role_scopes_list,
122
                )),
123
            )
124
6682
            .route(
125
6682
                "/{device_id}",
126
6682
                routing::get(api::get_device)
127
6682
                    .patch(api::patch_device)
128
6682
                    .delete(api::delete_device)
129
6682
                    .layer(AuthService::new(
130
6682
                        state.client.clone(),
131
6682
                        auth_uri.clone(),
132
6682
                        role_scopes_param,
133
                    )),
134
            )
135
6682
            .with_state(state.clone()),
136
    )
137
6682
}