1
use std::collections::HashMap;
2

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

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

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

            
15
3341
pub fn new_service(scope_path: &str, state: &State) -> Router {
16
3341
    let mut role_scopes_root: HashMap<Method, RoleScopeType> = HashMap::new();
17
3341
    let mut role_scopes_bulk: HashMap<Method, RoleScopeType> = HashMap::new();
18
3341
    let mut role_scopes_bulk_del: 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

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

            
64
3341
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
65
3341
    Router::new().nest(
66
3341
        scope_path,
67
3341
        Router::new()
68
3341
            .route(
69
3341
                "/",
70
3341
                routing::post(api::post_device_route)
71
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_root)),
72
3341
            )
73
3341
            .route(
74
3341
                "/bulk",
75
3341
                routing::post(api::post_device_route_bulk)
76
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk.clone())),
77
3341
            )
78
3341
            .route(
79
3341
                "/bulk-delete",
80
3341
                routing::post(api::post_device_route_bulk_del).layer(AuthService::new(
81
3341
                    auth_uri.clone(),
82
3341
                    role_scopes_bulk_del.clone(),
83
3341
                )),
84
3341
            )
85
3341
            .route(
86
3341
                "/range",
87
3341
                routing::post(api::post_device_route_range)
88
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk)),
89
3341
            )
90
3341
            .route(
91
3341
                "/range-delete",
92
3341
                routing::post(api::post_device_route_range_del)
93
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk_del)),
94
3341
            )
95
3341
            .route(
96
3341
                "/count",
97
3341
                routing::get(api::get_device_route_count)
98
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_count)),
99
3341
            )
100
3341
            .route(
101
3341
                "/list",
102
3341
                routing::get(api::get_device_route_list)
103
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_list)),
104
3341
            )
105
3341
            .route(
106
3341
                "/:route_id",
107
3341
                routing::delete(api::delete_device_route)
108
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_param)),
109
3341
            )
110
3341
            .with_state(state.clone()),
111
3341
    )
112
3341
}