refactor router into smaller ones
This commit is contained in:
parent
a1036aa02a
commit
56c918afaf
4 changed files with 33 additions and 14 deletions
|
@ -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,
|
||||
|
|
|
@ -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<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!
|
||||
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<Arc<AppState>>) -> impl IntoResponse
|
|||
|
||||
Ok(Json(json!({"username": username, "password": password})))
|
||||
}
|
||||
|
||||
pub fn router() -> Router<Arc<AppState>> {
|
||||
Router::new().route("/bootstrap", post(bootstrap))
|
||||
}
|
||||
|
|
|
@ -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<Arc<AppState>>,
|
||||
Json(payload): Json<LoginRequest>,
|
||||
) -> impl IntoResponse {
|
||||
|
@ -71,11 +72,11 @@ pub async fn login(
|
|||
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)
|
||||
}
|
||||
|
||||
pub async fn logout(
|
||||
async fn logout(
|
||||
State(state): State<Arc<AppState>>,
|
||||
RequireSession(session): RequireSession,
|
||||
) -> Result<impl IntoResponse, AppError<'static>> {
|
||||
|
@ -89,3 +90,10 @@ pub async fn logout(
|
|||
);
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub fn router() -> Router<Arc<AppState>> {
|
||||
Router::new()
|
||||
.route("/login", post(login))
|
||||
.route("/logout", post(logout))
|
||||
.route("/me", get(me))
|
||||
}
|
||||
|
|
|
@ -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<Arc<AppState>>,
|
||||
Path((site, slug)): Path<(String, String)>,
|
||||
) -> Result<String, AppError<'static>> {
|
||||
|
@ -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<Arc<AppState>> {
|
||||
Router::new().route("/:site/:slug", get(get_page))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue