1
use std::sync::Arc;
2

            
3
use axum::{
4
    Router, middleware,
5
    routing::{get, post},
6
};
7

            
8
#[cfg(feature = "scripting")]
9
use crate::pages::script;
10
use crate::{
11
    AppState,
12
    files::download_file,
13
    handler::{
14
        get_home_link, get_logout_link, get_me_handler, get_version, login_user_handler,
15
        logout_handler, refresh_access_token_handler, register_user_handler,
16
    },
17
    jwt_auth::{admin, auth},
18
    pages::{account, commodity, file_table, tag},
19
    redirect_middleware::redirect_on_auth_error,
20
};
21

            
22
1
pub fn create_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
23
1
    Router::new()
24
1
        .route(
25
1
            "/commodity/create",
26
1
            get(crate::pages::commodity::create::submit::commodity_create_page),
27
        )
28
1
        .route(
29
1
            "/commodity/list",
30
1
            get(crate::pages::commodity::list::commodity_list_page),
31
        )
32
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
33
1
        .route_layer(middleware::from_fn(redirect_on_auth_error))
34
1
        .with_state(app_state.clone())
35
1
}
36

            
37
1
pub fn create_accounts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
38
1
    Router::new()
39
1
        .route(
40
1
            "/account/create",
41
1
            get(crate::pages::account::create::submit::account_create_page),
42
        )
43
1
        .route(
44
1
            "/account/list",
45
1
            get(crate::pages::account::list::account_list_page),
46
        )
47
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
48
1
        .route_layer(middleware::from_fn(redirect_on_auth_error))
49
1
        .with_state(app_state.clone())
50
1
}
51

            
52
1
pub fn create_transactions_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
53
1
    Router::new()
54
1
        .route(
55
1
            "/transaction/create",
56
1
            get(crate::pages::transaction::create::submit::transaction_create_page),
57
        )
58
1
        .route(
59
1
            "/transaction/list",
60
1
            get(crate::pages::transaction::list::transaction_list_page),
61
        )
62
1
        .route(
63
1
            "/transaction/edit/{id}",
64
1
            get(crate::pages::transaction::edit::submit::transaction_edit_page),
65
        )
66
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
67
1
        .route_layer(middleware::from_fn(redirect_on_auth_error))
68
1
        .with_state(app_state.clone())
69
1
}
70

            
71
pub fn create_tags_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
72
    Router::new()
73
        .route(
74
            "/tag/create",
75
            get(crate::pages::tag::create::tag_create_page),
76
        )
77
        .route("/tag/list", get(crate::pages::tag::list::tag_list_page))
78
        .route(
79
            "/tag/edit/{id}",
80
            get(crate::pages::tag::edit::tag_edit_page),
81
        )
82
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
83
        .route_layer(middleware::from_fn(redirect_on_auth_error))
84
        .with_state(app_state.clone())
85
}
86

            
87
#[cfg(feature = "scripting")]
88
pub fn create_scripts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
89
    Router::new()
90
        .route("/script/create", get(script::edit::script_create_page))
91
        .route("/script/list", get(script::list::script_list_page))
92
        .route("/script/edit/{id}", get(script::edit::script_edit_page))
93
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
94
        .route_layer(middleware::from_fn(redirect_on_auth_error))
95
        .with_state(app_state.clone())
96
}
97

            
98
3
pub fn create_api_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
99
3
    let public_router = Router::new()
100
3
        .route("/auth/login", post(login_user_handler))
101
3
        .route("/version", get(get_version));
102

            
103
3
    let auth_router = Router::new()
104
3
        .route("/auth/refresh", get(refresh_access_token_handler))
105
3
        .route("/files/list", get(file_table))
106
3
        .route("/files/download/{file_name}", get(download_file))
107
3
        .route("/auth/logout", get(logout_handler))
108
3
        .route("/users/me", get(get_me_handler))
109
3
        .route("/htmx/logoutlink", get(get_logout_link))
110
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
111
3
        .with_state(app_state.clone());
112

            
113
3
    let account_router = Router::new()
114
3
        .route("/account/list", get(account::list::account_table))
115
3
        .route("/account/info", get(account::list::account_info))
116
3
        .route("/account/search", post(account::search::search_accounts))
117
3
        .route(
118
3
            "/account/create/submit",
119
3
            post(account::create::submit::create_account),
120
        )
121
3
        .route(
122
3
            "/account/create/validate/name",
123
3
            post(account::create::validate::validate_name),
124
        )
125
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
126
3
        .with_state(app_state.clone());
127

            
128
3
    let nav_router = Router::new()
129
3
        .route("/nav/home", get(get_home_link))
130
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
131
3
        .with_state(app_state.clone());
132

            
133
3
    let commodity_router = Router::new()
134
3
        .route("/commodity/list", get(commodity::list::commodity_table))
135
3
        .route(
136
3
            "/commodity/search",
137
3
            post(commodity::search::search_commodities),
138
        )
139
3
        .route(
140
3
            "/commodity/create/submit",
141
3
            post(commodity::create::submit::commodity_submit),
142
        )
143
3
        .route(
144
3
            "/commodity/create/validate/fraction",
145
3
            post(commodity::create::validate::validate_fraction),
146
        )
147
3
        .route(
148
3
            "/commodity/create/validate/symbol",
149
3
            post(commodity::create::validate::validate_symbol),
150
        )
151
3
        .route(
152
3
            "/commodity/create/validate/name",
153
3
            post(commodity::create::validate::validate_name),
154
        )
155
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
156
3
        .with_state(app_state.clone());
157

            
158
3
    let transaction_router = Router::new()
159
3
        .route(
160
3
            "/transaction/list",
161
3
            get(crate::pages::transaction::list::transaction_table),
162
        )
163
3
        .route(
164
3
            "/transaction/split/create",
165
3
            get(crate::pages::transaction::create::split::split_create_handler),
166
        )
167
3
        .route(
168
3
            "/transaction/create/submit",
169
3
            post(crate::pages::transaction::create::submit::transaction_submit),
170
        )
171
3
        .route(
172
3
            "/transaction/create/validate/amount",
173
3
            post(crate::pages::transaction::validate::validate_amount_html),
174
        )
175
3
        .route(
176
3
            "/transaction/create/validate/from_account",
177
3
            post(crate::pages::transaction::validate::validate_from_account_html),
178
        )
179
3
        .route(
180
3
            "/transaction/create/validate/to_account",
181
3
            post(crate::pages::transaction::validate::validate_to_account_html),
182
        )
183
3
        .route(
184
3
            "/transaction/create/validate/note",
185
3
            post(crate::pages::transaction::validate::validate_note_html),
186
        )
187
3
        .route(
188
3
            "/transaction/edit/submit",
189
3
            post(crate::pages::transaction::edit::submit::transaction_edit_submit),
190
        )
191
3
        .route(
192
3
            "/transaction/edit/validate/amount",
193
3
            post(crate::pages::transaction::validate::validate_amount_json),
194
        )
195
3
        .route(
196
3
            "/transaction/edit/validate/from_account",
197
3
            post(crate::pages::transaction::validate::validate_from_account_json),
198
        )
199
3
        .route(
200
3
            "/transaction/edit/validate/to_account",
201
3
            post(crate::pages::transaction::validate::validate_to_account_json),
202
        )
203
3
        .route(
204
3
            "/transaction/edit/validate/note",
205
3
            post(crate::pages::transaction::validate::validate_note_json),
206
        )
207
3
        .route(
208
3
            "/transaction/delete/{id}",
209
3
            post(crate::pages::transaction::delete::submit::transaction_delete_submit),
210
        )
211
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
212
3
        .with_state(app_state.clone());
213

            
214
3
    let tag_router = Router::new()
215
3
        .route("/tag/list", get(tag::list::tag_table))
216
3
        .route("/tag/create/submit", post(tag::create::create_tag))
217
3
        .route(
218
3
            "/transaction/{id}/tag/create/submit",
219
3
            post(tag::create::create_transaction_tag),
220
        )
221
3
        .route(
222
3
            "/split/{id}/tag/create/submit",
223
3
            post(tag::create::create_split_tag),
224
        )
225
3
        .route("/tag/edit/submit", post(tag::edit::edit_tag))
226
3
        .route("/tag/delete/{id}", post(tag::edit::delete_tag))
227
3
        .route(
228
3
            "/tags/transaction/names",
229
3
            get(tag::autocomplete::transaction_tag_names),
230
        )
231
3
        .route(
232
3
            "/tags/transaction/values",
233
3
            get(tag::autocomplete::transaction_tag_values),
234
        )
235
3
        .route("/tags/split/names", get(tag::autocomplete::split_tag_names))
236
3
        .route(
237
3
            "/tags/split/values",
238
3
            get(tag::autocomplete::split_tag_values),
239
        )
240
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
241
3
        .with_state(app_state.clone());
242

            
243
    #[cfg(feature = "scripting")]
244
3
    let script_router = Router::new()
245
3
        .route("/script/list", get(script::list::script_table))
246
3
        .route("/script/create/submit", post(script::edit::create_script))
247
3
        .route("/script/edit/submit", post(script::edit::edit_script))
248
3
        .route(
249
3
            "/script/bytecode/{id}",
250
3
            post(script::edit::upload_script_bytecode),
251
        )
252
3
        .route("/script/delete/{id}", post(script::edit::delete_script))
253
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
254
3
        .with_state(app_state.clone());
255

            
256
3
    let admin_router = Router::new()
257
3
        .route("/auth/register", post(register_user_handler))
258
3
        .route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
259
3
        .route_layer(middleware::from_fn_with_state(app_state, auth));
260

            
261
3
    let router = Router::new()
262
3
        .merge(public_router)
263
3
        .merge(nav_router)
264
3
        .merge(auth_router)
265
3
        .merge(account_router)
266
3
        .merge(commodity_router)
267
3
        .merge(transaction_router)
268
3
        .merge(tag_router)
269
3
        .merge(admin_router);
270

            
271
    #[cfg(feature = "scripting")]
272
3
    let router = router.merge(script_router);
273

            
274
3
    router
275
3
}