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

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

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