refactor router into smaller ones

This commit is contained in:
Hamcha 2023-07-02 19:27:16 +02:00
parent a1036aa02a
commit 56c918afaf
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
4 changed files with 33 additions and 14 deletions

View file

@ -40,11 +40,9 @@ async fn main() -> Result<()> {
let shared_state = Arc::new(AppState { database, config }); let shared_state = Arc::new(AppState { database, config });
let app = Router::new() let app = Router::new()
.route("/auth/login", post(routes::auth::login)) .nest("/auth", routes::auth::router())
.route("/auth/logout", post(routes::auth::logout)) .nest("/admin", routes::admin::router())
.route("/me", get(routes::auth::me)) .nest("/pages", routes::content::router())
.route("/pages/:site/:slug", get(routes::content::page))
.route("/admin/bootstrap", post(routes::admin::bootstrap))
.route_layer(middleware::from_fn_with_state( .route_layer(middleware::from_fn_with_state(
shared_state.clone(), shared_state.clone(),
refresh_sessions, refresh_sessions,

View file

@ -1,7 +1,8 @@
use axum::extract::State;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::routing::post;
use axum::Json; use axum::Json;
use axum::{extract::State, Router};
use serde_json::json; use serde_json::json;
use std::sync::Arc; use std::sync::Arc;
@ -12,7 +13,7 @@ use crate::{
state::AppState, state::AppState,
}; };
pub async fn bootstrap(State(state): State<Arc<AppState>>) -> impl IntoResponse { async fn bootstrap(State(state): State<Arc<AppState>>) -> impl IntoResponse {
// Only allow this request if the user table is completely empty! // Only allow this request if the user table is completely empty!
let empty = sqlx::query!( let empty = sqlx::query!(
"SELECT CASE WHEN EXISTS(SELECT 1 FROM users) THEN false ELSE true END AS empty;" "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<Arc<AppState>>) -> impl IntoResponse
Ok(Json(json!({"username": username, "password": password}))) Ok(Json(json!({"username": username, "password": password})))
} }
pub fn router() -> Router<Arc<AppState>> {
Router::new().route("/bootstrap", post(bootstrap))
}

View file

@ -2,7 +2,8 @@ use axum::{
extract::State, extract::State,
http::{header::SET_COOKIE, StatusCode}, http::{header::SET_COOKIE, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Json, routing::{get, post},
Json, Router,
}; };
use chrono::Duration; use chrono::Duration;
use serde::Deserialize; use serde::Deserialize;
@ -21,12 +22,12 @@ use crate::{
}; };
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct LoginRequest { struct LoginRequest {
pub username: String, pub username: String,
pub password: String, pub password: String,
} }
pub async fn login( async fn login(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Json(payload): Json<LoginRequest>, Json(payload): Json<LoginRequest>,
) -> impl IntoResponse { ) -> impl IntoResponse {
@ -71,11 +72,11 @@ pub async fn login(
Ok(response) Ok(response)
} }
pub async fn me(RequireUser(user): RequireUser) -> Result<String, AppError<'static>> { async fn me(RequireUser(user): RequireUser) -> Result<String, AppError<'static>> {
Ok(user.name) Ok(user.name)
} }
pub async fn logout( async fn logout(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
RequireSession(session): RequireSession, RequireSession(session): RequireSession,
) -> Result<impl IntoResponse, AppError<'static>> { ) -> Result<impl IntoResponse, AppError<'static>> {
@ -89,3 +90,10 @@ pub async fn logout(
); );
Ok(response) Ok(response)
} }
pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/login", post(login))
.route("/logout", post(logout))
.route("/me", get(me))
}

View file

@ -1,9 +1,13 @@
use axum::extract::{Path, State}; use axum::{
extract::{Path, State},
routing::get,
Router,
};
use std::sync::Arc; use std::sync::Arc;
use crate::{content::Page, error::AppError, state::AppState}; use crate::{content::Page, error::AppError, state::AppState};
pub async fn page( async fn get_page(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Path((site, slug)): Path<(String, String)>, Path((site, slug)): Path<(String, String)>,
) -> Result<String, AppError<'static>> { ) -> Result<String, AppError<'static>> {
@ -15,3 +19,7 @@ pub async fn page(
let page: Page = page_query.fetch_one(&state.database).await?; let page: Page = page_query.fetch_one(&state.database).await?;
Ok(page.title) Ok(page.title)
} }
pub fn router() -> Router<Arc<AppState>> {
Router::new().route("/:site/:slug", get(get_page))
}