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
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
6682

            
23
6682
    match state.api_scopes.get("device-route.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-route.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-route.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-route.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_route)
71
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_root)),
72
6682
            )
73
6682
            .route(
74
6682
                "/bulk",
75
6682
                routing::post(api::post_device_route_bulk)
76
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk.clone())),
77
6682
            )
78
6682
            .route(
79
6682
                "/bulk-delete",
80
6682
                routing::post(api::post_device_route_bulk_del).layer(AuthService::new(
81
6682
                    auth_uri.clone(),
82
6682
                    role_scopes_bulk_del.clone(),
83
6682
                )),
84
6682
            )
85
6682
            .route(
86
6682
                "/range",
87
6682
                routing::post(api::post_device_route_range)
88
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk)),
89
6682
            )
90
6682
            .route(
91
6682
                "/range-delete",
92
6682
                routing::post(api::post_device_route_range_del)
93
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_bulk_del)),
94
6682
            )
95
6682
            .route(
96
6682
                "/count",
97
6682
                routing::get(api::get_device_route_count)
98
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_count)),
99
6682
            )
100
6682
            .route(
101
6682
                "/list",
102
6682
                routing::get(api::get_device_route_list)
103
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_list)),
104
6682
            )
105
6682
            .route(
106
6682
                "/{route_id}",
107
6682
                routing::delete(api::delete_device_route)
108
6682
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_param)),
109
6682
            )
110
6682
            .with_state(state.clone()),
111
6682
    )
112
6682
}