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_count: HashMap<Method, RoleScopeType> = HashMap::new();
18
3341
    let mut role_scopes_list: HashMap<Method, RoleScopeType> = HashMap::new();
19
3341
    let mut role_scopes_param: HashMap<Method, RoleScopeType> = HashMap::new();
20
3341

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

            
58
3341
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
59
3341
    Router::new().nest(
60
3341
        scope_path,
61
3341
        Router::new()
62
3341
            .route(
63
3341
                "/",
64
3341
                routing::post(api::post_network)
65
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_root)),
66
3341
            )
67
3341
            .route(
68
3341
                "/count",
69
3341
                routing::get(api::get_network_count)
70
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_count)),
71
3341
            )
72
3341
            .route(
73
3341
                "/list",
74
3341
                routing::get(api::get_network_list)
75
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_list)),
76
3341
            )
77
3341
            .route(
78
3341
                "/:network_id",
79
3341
                routing::get(api::get_network)
80
3341
                    .patch(api::patch_network)
81
3341
                    .delete(api::delete_network)
82
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_param)),
83
3341
            )
84
3341
            .with_state(state.clone()),
85
3341
    )
86
3341
}