From 56c918afaf49b3b999b12807881d39fc4c89aac7 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sun, 2 Jul 2023 19:27:16 +0200 Subject: [PATCH] refactor router into smaller ones --- src/main.rs | 8 +++----- src/routes/admin.rs | 9 +++++++-- src/routes/auth.rs | 18 +++++++++++++----- src/routes/content.rs | 12 ++++++++++-- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 699629a..fda920c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,11 +40,9 @@ async fn main() -> Result<()> { let shared_state = Arc::new(AppState { database, config }); let app = Router::new() - .route("/auth/login", post(routes::auth::login)) - .route("/auth/logout", post(routes::auth::logout)) - .route("/me", get(routes::auth::me)) - .route("/pages/:site/:slug", get(routes::content::page)) - .route("/admin/bootstrap", post(routes::admin::bootstrap)) + .nest("/auth", routes::auth::router()) + .nest("/admin", routes::admin::router()) + .nest("/pages", routes::content::router()) .route_layer(middleware::from_fn_with_state( shared_state.clone(), refresh_sessions, diff --git a/src/routes/admin.rs b/src/routes/admin.rs index d23a6e7..e0d6739 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -1,7 +1,8 @@ -use axum::extract::State; use axum::http::StatusCode; use axum::response::IntoResponse; +use axum::routing::post; use axum::Json; +use axum::{extract::State, Router}; use serde_json::json; use std::sync::Arc; @@ -12,7 +13,7 @@ use crate::{ state::AppState, }; -pub async fn bootstrap(State(state): State>) -> impl IntoResponse { +async fn bootstrap(State(state): State>) -> impl IntoResponse { // Only allow this request if the user table is completely empty! let empty = sqlx::query!( "SELECT CASE WHEN EXISTS(SELECT 1 FROM users) THEN false ELSE true END AS empty;" @@ -44,3 +45,7 @@ pub async fn bootstrap(State(state): State>) -> impl IntoResponse Ok(Json(json!({"username": username, "password": password}))) } + +pub fn router() -> Router> { + Router::new().route("/bootstrap", post(bootstrap)) +} diff --git a/src/routes/auth.rs b/src/routes/auth.rs index 69b8807..c723331 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -2,7 +2,8 @@ use axum::{ extract::State, http::{header::SET_COOKIE, StatusCode}, response::{IntoResponse, Response}, - Json, + routing::{get, post}, + Json, Router, }; use chrono::Duration; use serde::Deserialize; @@ -21,12 +22,12 @@ use crate::{ }; #[derive(Deserialize)] -pub struct LoginRequest { +struct LoginRequest { pub username: String, pub password: String, } -pub async fn login( +async fn login( State(state): State>, Json(payload): Json, ) -> impl IntoResponse { @@ -71,11 +72,11 @@ pub async fn login( Ok(response) } -pub async fn me(RequireUser(user): RequireUser) -> Result> { +async fn me(RequireUser(user): RequireUser) -> Result> { Ok(user.name) } -pub async fn logout( +async fn logout( State(state): State>, RequireSession(session): RequireSession, ) -> Result> { @@ -89,3 +90,10 @@ pub async fn logout( ); Ok(response) } + +pub fn router() -> Router> { + Router::new() + .route("/login", post(login)) + .route("/logout", post(logout)) + .route("/me", get(me)) +} diff --git a/src/routes/content.rs b/src/routes/content.rs index dab2589..b05b976 100644 --- a/src/routes/content.rs +++ b/src/routes/content.rs @@ -1,9 +1,13 @@ -use axum::extract::{Path, State}; +use axum::{ + extract::{Path, State}, + routing::get, + Router, +}; use std::sync::Arc; use crate::{content::Page, error::AppError, state::AppState}; -pub async fn page( +async fn get_page( State(state): State>, Path((site, slug)): Path<(String, String)>, ) -> Result> { @@ -15,3 +19,7 @@ pub async fn page( let page: Page = page_query.fetch_one(&state.database).await?; Ok(page.title) } + +pub fn router() -> Router> { + Router::new().route("/:site/:slug", get(get_page)) +}