1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::mode::mock::ContextLock;
use jortestkit::web::api_token::API_TOKEN_HEADER;
use warp::filters::BoxedFilter;
use warp::{Filter, Rejection, Reply};

use super::handlers::*;

pub async fn control_filter(
    context: ContextLock,
    root: BoxedFilter<()>,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
    let working_dir = context.read().unwrap().working_dir();
    let is_token_enabled = context.read().unwrap().api_token().is_some();

    let with_context = warp::any().map(move || context.clone());

    let api_token_filter = if is_token_enabled {
        warp::header::header(API_TOKEN_HEADER)
            .and(with_context.clone())
            .and_then(authorize_token)
            .and(warp::any())
            .untuple_one()
            .boxed()
    } else {
        warp::any().boxed()
    };

    let files = {
        let root = warp::path!("files" / ..).boxed();

        let get = warp::path("get").and(warp::fs::dir(working_dir));
        let list = warp::path!("list")
            .and(warp::get())
            .and(with_context.clone())
            .and_then(file_lister_handler);

        root.and(get.or(list)).boxed()
    };

    let command = {
        let root = warp::path!("command" / ..);

        let reset = warp::path!("reset")
            .and(warp::post())
            .and(with_context.clone())
            .and(warp::body::json())
            .and_then(command_reset_mock);

        let availability = warp::path!("available" / bool)
            .and(warp::post())
            .and(with_context.clone())
            .and_then(command_available);

        let set_error_code = warp::path!("error-code" / u16)
            .and(warp::post())
            .and(with_context.clone())
            .and_then(command_error_code);

        let fund = {
            let root = warp::path!("fund");

            let fund_id = warp::path!("id" / i32)
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_fund_id);

            let fund_update = warp::path!("update")
                .and(warp::put())
                .and(warp::body::json())
                .and(with_context.clone())
                .and_then(command_update_fund);

            root.and(fund_id.or(fund_update))
        };

        let version = warp::path!("version" / String)
            .and(warp::post())
            .and(with_context.clone())
            .and_then(command_version);

        let block_account = {
            let root = warp::path!("block-account" / ..);

            let block_counter = warp::path!(u32)
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_block_account);

            let reset = warp::path!("reset")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_reset_block_account);

            root.and(block_counter.or(reset)).boxed()
        };

        let fragment_strategy = {
            let root = warp::path!("fragments" / ..);

            let reject = warp::path!("reject")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_reject);

            let accept = warp::path!("accept")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_accept);

            let pending = warp::path!("pending")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_pending);

            let reset = warp::path!("reset")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_reset);

            let forget = warp::path!("forget")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_forget);

            let update = {
                let root = warp::path!("update" / ..);

                let reject = warp::path!(String / "reject")
                    .and(warp::post())
                    .and(with_context.clone())
                    .and_then(command_update_reject);

                let accept = warp::path!(String / "accept")
                    .and(warp::post())
                    .and(with_context.clone())
                    .and_then(command_update_accept);

                let pending = warp::path!(String / "pending")
                    .and(warp::post())
                    .and(with_context.clone())
                    .and_then(command_update_pending);

                let forget = warp::path!(String / "forget")
                    .and(warp::post())
                    .and(with_context.clone())
                    .and_then(command_update_forget);

                root.and(reject.or(accept).or(pending).or(forget)).boxed()
            };

            root.and(
                reject
                    .or(accept)
                    .or(pending)
                    .or(reset)
                    .or(update)
                    .or(forget),
            )
            .boxed()
        };

        let network_strategy = {
            let root = warp::path!("congestion" / ..);

            let normal = warp::path!("normal")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_congestion_normal);

            let jammed = warp::path!("jammed")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_congestion_jammed);

            let moderate = warp::path!("moderate")
                .and(warp::post())
                .and(with_context.clone())
                .and_then(command_congestion_moderate);

            let reset = warp::path!("reset")
                .and(warp::post())
                .and(with_context)
                .and_then(command_congestion_reset);

            root.and(normal.or(jammed).or(moderate).or(reset)).boxed()
        };

        let snapshot_service = {
            let root = warp::path!("snapshot" / ..);

            let create = warp::path!("create")
                .and(warp::post())
                .and(warp::body::json())
                .and_then(command_create_snapshot);

            root.and(create).boxed()
        };

        root.and(
            reset
                .or(availability)
                .or(set_error_code)
                .or(fund)
                .or(block_account)
                .or(fragment_strategy)
                .or(network_strategy)
                .or(version)
                .or(snapshot_service),
        )
        .boxed()
    };
    root.and(api_token_filter).and(command.or(files))
}