Skip to main content

web/
route.rs

1use std::sync::Arc;
2
3use axum::{
4    Router, middleware,
5    routing::{get, post},
6};
7
8use crate::pages::script;
9use crate::pages::template;
10use crate::{
11    AppState,
12    error_render::full_page,
13    handler::{get_home_link, get_logout_link, get_version},
14    jwt_auth::{admin, auth},
15    login::{login_form_handler, login_user_handler, reshow_login},
16    pages::{account, commodity, report, tag},
17    rate_limit::{AuthLimiter, throttle_auth},
18    register::register_user_handler,
19    session::{change_password_handler, logout_handler, refresh_access_token_handler},
20};
21
22/// Pages only an admin may see.
23///
24/// Registration is invite-only: `POST /api/auth/register` has always been
25/// admin-gated, but the page carrying its form was public, so an anonymous
26/// visitor was handed a signup form whose every submission is refused. The
27/// page now sits behind the same gate as the endpoint it posts to.
28///
29/// Layer order matters: `auth` must run before `admin`, which reads the
30/// extension `auth` inserts.
31pub fn create_admin_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
32    Router::new()
33        .route("/register", get(crate::pages::register))
34        .route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
35        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
36        .route_layer(middleware::from_fn(full_page))
37        .with_state(app_state.clone())
38}
39
40pub fn create_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
41    Router::new()
42        .route(
43            "/commodity/create",
44            get(crate::pages::commodity::create::commodity_create_page),
45        )
46        .route(
47            "/commodity/convert",
48            get(crate::pages::commodity::convert::commodity_convert_page),
49        )
50        .route(
51            "/commodity/list",
52            get(crate::pages::commodity::list::commodity_list_page),
53        )
54        .route(
55            "/commodity/{id}/edit",
56            get(crate::pages::commodity::edit::commodity_edit_page),
57        )
58        .route("/config", get(crate::pages::config::config_page))
59        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
60        .route_layer(middleware::from_fn(full_page))
61        .with_state(app_state.clone())
62}
63
64pub fn create_accounts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
65    Router::new()
66        .route(
67            "/account/create",
68            get(crate::pages::account::create::account_create_page),
69        )
70        .route(
71            "/account/list",
72            get(crate::pages::account::list::account_list_page),
73        )
74        .route(
75            "/account/manage",
76            get(crate::pages::account::manage::account_manage_page),
77        )
78        .route(
79            "/account/ssh-key",
80            get(crate::pages::account::ssh_key::ssh_key_page),
81        )
82        .route(
83            crate::pages::account::merge::PAGE_PATH,
84            get(crate::pages::account::merge::account_merge_page),
85        )
86        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
87        .route_layer(middleware::from_fn(full_page))
88        .with_state(app_state.clone())
89}
90
91pub fn create_transactions_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
92    Router::new()
93        .route(
94            "/transaction/new",
95            get(crate::pages::transaction::new::transaction_new_page),
96        )
97        .route(
98            crate::pages::transaction::browse::LIST_PATH,
99            get(crate::pages::transaction::browse::transaction_list_page),
100        )
101        .route(
102            "/transaction/{id}/edit",
103            get(crate::pages::transaction::update::transaction_update_page),
104        )
105        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
106        .route_layer(middleware::from_fn(full_page))
107        .with_state(app_state.clone())
108}
109
110pub fn create_tags_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
111    Router::new()
112        .route(
113            "/tag/create",
114            get(crate::pages::tag::create::tag_create_page),
115        )
116        .route("/tag/list", get(crate::pages::tag::list::tag_list_page))
117        .route(
118            "/tag/edit/{id}",
119            get(crate::pages::tag::edit::tag_edit_page),
120        )
121        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
122        .route_layer(middleware::from_fn(full_page))
123        .with_state(app_state.clone())
124}
125
126pub fn create_scripts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
127    Router::new()
128        .route("/script/create", get(script::create::script_create_page))
129        .route("/script/list", get(script::list::script_list_page))
130        .route("/script/edit/{id}", get(script::edit::script_edit_page))
131        .route(
132            "/template/create",
133            get(template::create::template_create_page),
134        )
135        .route("/template/list", get(template::list::template_list_page))
136        .route(
137            "/template/edit/{id}",
138            get(template::edit::template_edit_page),
139        )
140        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
141        .route_layer(middleware::from_fn(full_page))
142        .with_state(app_state.clone())
143}
144
145pub fn create_reports_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
146    Router::new()
147        .route("/report/balance", get(report::balance::balance_report_page))
148        .route(
149            "/report/activity",
150            get(report::activity::activity_report_page),
151        )
152        .route(
153            "/report/category-breakdown",
154            get(report::category_breakdown::category_breakdown_report_page),
155        )
156        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
157        .route_layer(middleware::from_fn(report::retired::reject_retired_filter))
158        .route_layer(middleware::from_fn(full_page))
159        .with_state(app_state.clone())
160}
161
162pub fn create_api_router(
163    app_state: Arc<AppState>,
164    limiter: Arc<AuthLimiter>,
165) -> Router<Arc<AppState>> {
166    // The login routes verify a password against a hash for anyone who asks,
167    // so they carry a per-address throttle. `/version` is outside it: it reads
168    // nothing and costs nothing, and throttling it would only make an unrelated
169    // health check share a budget with the credential path.
170    // The form is the one POST that answers an error by showing itself again,
171    // so `reshow_login` sits outside the throttle to cover its refusal too.
172    let login_form_router = Router::new()
173        .route("/auth/login", post(login_form_handler))
174        .route_layer(middleware::from_fn_with_state(
175            limiter.clone(),
176            throttle_auth,
177        ))
178        .route_layer(middleware::from_fn(reshow_login));
179
180    let login_json_router = Router::new()
181        .route("/auth/login/json", post(login_user_handler))
182        .route_layer(middleware::from_fn_with_state(
183            limiter.clone(),
184            throttle_auth,
185        ));
186
187    let public_router = login_form_router
188        .merge(login_json_router)
189        .route("/version", get(get_version));
190
191    // Both layers: it verifies a password, so it belongs behind the same
192    // throttle as login, and it acts on the caller, so it needs `auth`.
193    let password_router = Router::new()
194        .route("/auth/password", post(change_password_handler))
195        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
196        .route_layer(middleware::from_fn_with_state(limiter, throttle_auth))
197        .with_state(app_state.clone());
198
199    let auth_router = Router::new()
200        .route("/auth/refresh", get(refresh_access_token_handler))
201        .route("/auth/logout", get(logout_handler))
202        .route("/htmx/logoutlink", get(get_logout_link))
203        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
204        .with_state(app_state.clone());
205
206    let account_router = Router::new()
207        .route("/account/list", get(account::list::account_table))
208        .route("/account/info", get(account::list::account_info))
209        .route(
210            "/account/manage/view",
211            get(account::manage::account_manage_view),
212        )
213        .route(
214            "/account/manage/submit",
215            post(account::manage::account_manage_submit),
216        )
217        .route(
218            "/account/create/submit",
219            post(account::create::account_create_submit),
220        )
221        .route("/account/ssh-key/add", post(account::ssh_key::ssh_key_add))
222        .route(
223            "/account/merge/review",
224            post(account::merge::account_merge_review),
225        )
226        .route(
227            "/account/merge/submit",
228            post(account::merge::account_merge_submit),
229        )
230        .route(
231            "/account/ssh-key/remove",
232            post(account::ssh_key::ssh_key_remove),
233        )
234        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
235        .with_state(app_state.clone());
236
237    let nav_router = Router::new()
238        .route("/nav/home", get(get_home_link))
239        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
240        .with_state(app_state.clone());
241
242    let commodity_router = Router::new()
243        .route("/commodity/list", get(commodity::list::commodity_table))
244        .route(
245            "/commodity/create/submit",
246            post(commodity::create::commodity_create_submit),
247        )
248        .route(
249            "/commodity/edit",
250            post(commodity::edit::commodity_edit_submit),
251        )
252        .route(
253            "/commodity/convert",
254            get(commodity::convert::commodity_convert),
255        )
256        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
257        .with_state(app_state.clone());
258
259    let transaction_router = Router::new()
260        .route(
261            "/transaction/detail/{id}",
262            get(crate::pages::transaction::browse::transaction_detail),
263        )
264        .route(
265            "/transaction/new/submit",
266            post(crate::pages::transaction::new::transaction_new_submit),
267        )
268        .route(
269            "/transaction/update",
270            post(crate::pages::transaction::update::transaction_update_submit),
271        )
272        .route(
273            "/transaction/delete/{id}",
274            post(crate::pages::transaction::delete::submit::transaction_delete_submit),
275        )
276        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
277        .with_state(app_state.clone());
278
279    let tag_router = Router::new()
280        .route("/tag/list", get(tag::list::tag_table))
281        .route("/tag/create/submit", post(tag::create::tag_create_submit))
282        .route("/tag/edit/submit", post(tag::edit::tag_edit_submit))
283        .route("/tag/delete/{id}", post(tag::edit::delete_tag))
284        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
285        .with_state(app_state.clone());
286
287    let config_router = Router::new()
288        .route("/config/list", get(crate::pages::config::config_table))
289        .route("/config/set", post(crate::pages::config::config_set_submit))
290        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
291        .with_state(app_state.clone());
292
293    let forms_router = Router::new()
294        .route(
295            "/forms/options/{source}",
296            get(crate::pages::forms::options::form_options),
297        )
298        .route(
299            "/forms/suggestions/{source}",
300            get(crate::pages::forms::suggestions::form_suggestions),
301        )
302        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
303        .with_state(app_state.clone());
304
305    let script_router = Router::new()
306        .route("/script/list", get(script::list::script_table))
307        .route(
308            "/script/create/submit",
309            post(script::create::script_create_submit),
310        )
311        .route(
312            "/script/edit/submit",
313            post(script::edit::script_edit_submit),
314        )
315        .route("/script/delete/{id}", post(script::edit::delete_script))
316        .route(
317            "/account/{account_id}/script/run/{script_id}",
318            post(account::script::run_account_script),
319        )
320        .route("/template/list", get(template::list::template_table))
321        .route(
322            "/template/create/submit",
323            post(template::create::template_create_submit),
324        )
325        .route("/template/test", post(template::draft::template_test))
326        .route(
327            "/template/edit/submit",
328            post(template::edit::template_edit_submit),
329        )
330        .route(
331            "/template/delete/{id}",
332            post(template::edit::delete_template),
333        )
334        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
335        .with_state(app_state.clone());
336
337    let report_router = Router::new()
338        .route(
339            "/report/balance",
340            get(report::balance::balance_report_table),
341        )
342        .route(
343            "/report/balance/chart.svg",
344            get(report::balance::balance_report_chart_svg),
345        )
346        .route(
347            "/report/balance/chart.json",
348            get(report::balance::balance_report_chart_json),
349        )
350        .route(
351            "/report/activity",
352            get(report::activity::activity_report_table),
353        )
354        .route(
355            "/report/activity/chart.svg",
356            get(report::activity::activity_report_chart_svg),
357        )
358        .route(
359            "/report/activity/chart.json",
360            get(report::activity::activity_report_chart_json),
361        )
362        .route(
363            "/report/category-breakdown",
364            get(report::category_breakdown::category_breakdown_report_table),
365        )
366        .route(
367            "/report/category-breakdown/chart.svg",
368            get(report::category_breakdown::category_breakdown_report_chart_svg),
369        )
370        .route(
371            "/report/category-breakdown/chart.json",
372            get(report::category_breakdown::category_breakdown_report_chart_json),
373        )
374        .route(
375            "/report/category-breakdown/export.csv",
376            get(report::category_breakdown_csv::category_breakdown_report_csv),
377        )
378        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
379        .route_layer(middleware::from_fn(report::bookmark::push_report_url))
380        .route_layer(middleware::from_fn(report::retired::reject_retired_filter))
381        .with_state(app_state.clone());
382
383    let admin_router = Router::new()
384        .route("/auth/register", post(register_user_handler))
385        .route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
386        .route_layer(middleware::from_fn_with_state(app_state, auth));
387
388    let router = Router::new()
389        .merge(public_router)
390        .merge(password_router)
391        .merge(nav_router)
392        .merge(auth_router)
393        .merge(account_router)
394        .merge(commodity_router)
395        .merge(transaction_router)
396        .merge(tag_router)
397        .merge(config_router)
398        .merge(forms_router)
399        .merge(report_router)
400        .merge(admin_router);
401
402    router.merge(script_router)
403}