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

            
14
3341
pub fn new_service(scope_path: &str, state: &State) -> Router {
15
3341
    let mut role_scopes_count: HashMap<Method, RoleScopeType> = HashMap::new();
16
3341
    let mut role_scopes_list: HashMap<Method, RoleScopeType> = HashMap::new();
17
3341
    let mut role_scopes_param: HashMap<Method, RoleScopeType> = HashMap::new();
18
3341

            
19
3341
    match state.api_scopes.get("dldata-buffer.get") {
20
3340
        None => {
21
3340
            role_scopes_count.insert(Method::GET, (vec![], vec![]));
22
3340
            role_scopes_list.insert(Method::GET, (vec![], vec![]));
23
3340
            role_scopes_param.insert(Method::GET, (vec![], vec![]));
24
3340
        }
25
1
        Some(scopes) => {
26
1
            role_scopes_count.insert(Method::GET, (vec![], scopes.clone()));
27
1
            role_scopes_list.insert(Method::GET, (vec![], scopes.clone()));
28
1
            role_scopes_param.insert(Method::GET, (vec![], scopes.clone()));
29
1
        }
30
    }
31
3341
    match state.api_scopes.get("dldata-buffer.patch") {
32
3340
        None => {
33
3340
            role_scopes_param.insert(Method::PATCH, (vec![], vec![]));
34
3340
        }
35
1
        Some(scopes) => {
36
1
            role_scopes_param.insert(Method::PATCH, (vec![], scopes.clone()));
37
1
        }
38
    }
39
3341
    match state.api_scopes.get("dldata-buffer.delete") {
40
3340
        None => {
41
3340
            role_scopes_param.insert(Method::DELETE, (vec![], vec![]));
42
3340
        }
43
1
        Some(scopes) => {
44
1
            role_scopes_param.insert(Method::DELETE, (vec![], scopes.clone()));
45
1
        }
46
    }
47

            
48
3341
    let auth_uri = format!("{}/api/v1/auth/tokeninfo", state.auth_base.as_str());
49
3341
    Router::new().nest(
50
3341
        scope_path,
51
3341
        Router::new()
52
3341
            .route(
53
3341
                "/count",
54
3341
                routing::get(api::get_dldata_buffer_count)
55
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_count)),
56
3341
            )
57
3341
            .route(
58
3341
                "/list",
59
3341
                routing::get(api::get_dldata_buffer_list)
60
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_list)),
61
3341
            )
62
3341
            .route(
63
3341
                "/:data_id",
64
3341
                routing::delete(api::delete_dldata_buffer)
65
3341
                    .layer(AuthService::new(auth_uri.clone(), role_scopes_param)),
66
3341
            )
67
3341
            .with_state(state.clone()),
68
3341
    )
69
3341
}