Lines
90.35 %
Functions
18.75 %
Branches
100 %
use std::sync::Arc;
use axum::{
Router, middleware,
routing::{get, post},
};
use crate::pages::script;
use crate::pages::template;
use crate::{
AppState,
error_render::full_page,
handler::{get_home_link, get_logout_link, get_version},
jwt_auth::{admin, auth},
login::{login_form_handler, login_user_handler, reshow_login},
pages::{account, commodity, report, tag},
rate_limit::{AuthLimiter, throttle_auth},
register::register_user_handler,
session::{change_password_handler, logout_handler, refresh_access_token_handler},
/// Pages only an admin may see.
///
/// Registration is invite-only: `POST /api/auth/register` has always been
/// admin-gated, but the page carrying its form was public, so an anonymous
/// visitor was handed a signup form whose every submission is refused. The
/// page now sits behind the same gate as the endpoint it posts to.
/// Layer order matters: `auth` must run before `admin`, which reads the
/// extension `auth` inserts.
pub fn create_admin_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
Router::new()
.route("/register", get(crate::pages::register))
.route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
.route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
.route_layer(middleware::from_fn(full_page))
.with_state(app_state.clone())
}
pub fn create_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
.route(
"/commodity/create",
get(crate::pages::commodity::create::commodity_create_page),
)
"/commodity/convert",
get(crate::pages::commodity::convert::commodity_convert_page),
"/commodity/list",
get(crate::pages::commodity::list::commodity_list_page),
"/commodity/{id}/edit",
get(crate::pages::commodity::edit::commodity_edit_page),
.route("/config", get(crate::pages::config::config_page))
pub fn create_accounts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
"/account/create",
get(crate::pages::account::create::account_create_page),
"/account/list",
get(crate::pages::account::list::account_list_page),
"/account/manage",
get(crate::pages::account::manage::account_manage_page),
"/account/ssh-key",
get(crate::pages::account::ssh_key::ssh_key_page),
crate::pages::account::merge::PAGE_PATH,
get(crate::pages::account::merge::account_merge_page),
pub fn create_transactions_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
"/transaction/new",
get(crate::pages::transaction::new::transaction_new_page),
crate::pages::transaction::browse::LIST_PATH,
get(crate::pages::transaction::browse::transaction_list_page),
"/transaction/{id}/edit",
get(crate::pages::transaction::update::transaction_update_page),
pub fn create_tags_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
"/tag/create",
get(crate::pages::tag::create::tag_create_page),
.route("/tag/list", get(crate::pages::tag::list::tag_list_page))
"/tag/edit/{id}",
get(crate::pages::tag::edit::tag_edit_page),
pub fn create_scripts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
.route("/script/create", get(script::create::script_create_page))
.route("/script/list", get(script::list::script_list_page))
.route("/script/edit/{id}", get(script::edit::script_edit_page))
"/template/create",
get(template::create::template_create_page),
.route("/template/list", get(template::list::template_list_page))
"/template/edit/{id}",
get(template::edit::template_edit_page),
pub fn create_reports_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
.route("/report/balance", get(report::balance::balance_report_page))
"/report/activity",
get(report::activity::activity_report_page),
"/report/category-breakdown",
get(report::category_breakdown::category_breakdown_report_page),
.route_layer(middleware::from_fn(report::retired::reject_retired_filter))
pub fn create_api_router(
app_state: Arc<AppState>,
limiter: Arc<AuthLimiter>,
) -> Router<Arc<AppState>> {
// The login routes verify a password against a hash for anyone who asks,
// so they carry a per-address throttle. `/version` is outside it: it reads
// nothing and costs nothing, and throttling it would only make an unrelated
// health check share a budget with the credential path.
// The form is the one POST that answers an error by showing itself again,
// so `reshow_login` sits outside the throttle to cover its refusal too.
let login_form_router = Router::new()
.route("/auth/login", post(login_form_handler))
.route_layer(middleware::from_fn_with_state(
limiter.clone(),
throttle_auth,
))
.route_layer(middleware::from_fn(reshow_login));
let login_json_router = Router::new()
.route("/auth/login/json", post(login_user_handler))
));
let public_router = login_form_router
.merge(login_json_router)
.route("/version", get(get_version));
// Both layers: it verifies a password, so it belongs behind the same
// throttle as login, and it acts on the caller, so it needs `auth`.
let password_router = Router::new()
.route("/auth/password", post(change_password_handler))
.route_layer(middleware::from_fn_with_state(limiter, throttle_auth))
.with_state(app_state.clone());
let auth_router = Router::new()
.route("/auth/refresh", get(refresh_access_token_handler))
.route("/auth/logout", get(logout_handler))
.route("/htmx/logoutlink", get(get_logout_link))
let account_router = Router::new()
.route("/account/list", get(account::list::account_table))
.route("/account/info", get(account::list::account_info))
"/account/manage/view",
get(account::manage::account_manage_view),
"/account/manage/submit",
post(account::manage::account_manage_submit),
"/account/create/submit",
post(account::create::account_create_submit),
.route("/account/ssh-key/add", post(account::ssh_key::ssh_key_add))
"/account/merge/review",
post(account::merge::account_merge_review),
"/account/merge/submit",
post(account::merge::account_merge_submit),
"/account/ssh-key/remove",
post(account::ssh_key::ssh_key_remove),
let nav_router = Router::new()
.route("/nav/home", get(get_home_link))
let commodity_router = Router::new()
.route("/commodity/list", get(commodity::list::commodity_table))
"/commodity/create/submit",
post(commodity::create::commodity_create_submit),
"/commodity/edit",
post(commodity::edit::commodity_edit_submit),
get(commodity::convert::commodity_convert),
let transaction_router = Router::new()
"/transaction/detail/{id}",
get(crate::pages::transaction::browse::transaction_detail),
"/transaction/new/submit",
post(crate::pages::transaction::new::transaction_new_submit),
"/transaction/update",
post(crate::pages::transaction::update::transaction_update_submit),
"/transaction/delete/{id}",
post(crate::pages::transaction::delete::submit::transaction_delete_submit),
let tag_router = Router::new()
.route("/tag/list", get(tag::list::tag_table))
.route("/tag/create/submit", post(tag::create::tag_create_submit))
.route("/tag/edit/submit", post(tag::edit::tag_edit_submit))
.route("/tag/delete/{id}", post(tag::edit::delete_tag))
let config_router = Router::new()
.route("/config/list", get(crate::pages::config::config_table))
.route("/config/set", post(crate::pages::config::config_set_submit))
let forms_router = Router::new()
"/forms/options/{source}",
get(crate::pages::forms::options::form_options),
"/forms/suggestions/{source}",
get(crate::pages::forms::suggestions::form_suggestions),
let script_router = Router::new()
.route("/script/list", get(script::list::script_table))
"/script/create/submit",
post(script::create::script_create_submit),
"/script/edit/submit",
post(script::edit::script_edit_submit),
.route("/script/delete/{id}", post(script::edit::delete_script))
"/account/{account_id}/script/run/{script_id}",
post(account::script::run_account_script),
.route("/template/list", get(template::list::template_table))
"/template/create/submit",
post(template::create::template_create_submit),
.route("/template/test", post(template::draft::template_test))
"/template/edit/submit",
post(template::edit::template_edit_submit),
"/template/delete/{id}",
post(template::edit::delete_template),
let report_router = Router::new()
"/report/balance",
get(report::balance::balance_report_table),
"/report/balance/chart.svg",
get(report::balance::balance_report_chart_svg),
"/report/balance/chart.json",
get(report::balance::balance_report_chart_json),
get(report::activity::activity_report_table),
"/report/activity/chart.svg",
get(report::activity::activity_report_chart_svg),
"/report/activity/chart.json",
get(report::activity::activity_report_chart_json),
get(report::category_breakdown::category_breakdown_report_table),
"/report/category-breakdown/chart.svg",
get(report::category_breakdown::category_breakdown_report_chart_svg),
"/report/category-breakdown/chart.json",
get(report::category_breakdown::category_breakdown_report_chart_json),
"/report/category-breakdown/export.csv",
get(report::category_breakdown_csv::category_breakdown_report_csv),
.route_layer(middleware::from_fn(report::bookmark::push_report_url))
let admin_router = Router::new()
.route("/auth/register", post(register_user_handler))
.route_layer(middleware::from_fn_with_state(app_state, auth));
let router = Router::new()
.merge(public_router)
.merge(password_router)
.merge(nav_router)
.merge(auth_router)
.merge(account_router)
.merge(commodity_router)
.merge(transaction_router)
.merge(tag_router)
.merge(config_router)
.merge(forms_router)
.merge(report_router)
.merge(admin_router);
router.merge(script_router)