small refactor for the varius app state and nesting of api paths
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 6m54s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 6m54s
This commit is contained in:
parent
4b9ef4691e
commit
1cb32e3d2c
5
src/app_state.rs
Normal file
5
src/app_state.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
#[allow(dead_code)]
|
||||
pub db: sqlx::PgPool,
|
||||
}
|
||||
10
src/http/api_router.rs
Normal file
10
src/http/api_router.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use crate::app_state::AppState;
|
||||
use axum::Router;
|
||||
|
||||
use crate::http::{auth_router, health_router};
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.nest("/health", health_router::router())
|
||||
.nest("/auth", auth_router::router())
|
||||
}
|
||||
17
src/http/auth_router.rs
Normal file
17
src/http/auth_router.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use axum::{Json, Router, routing::post};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::app_state::AppState;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HealthResponse {
|
||||
status: &'static str,
|
||||
}
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new().route("/register", post(register))
|
||||
}
|
||||
|
||||
async fn register() -> Json<HealthResponse> {
|
||||
Json(HealthResponse { status: "ok" })
|
||||
}
|
||||
@ -1,16 +1,15 @@
|
||||
use axum::{Json, Router, routing::get};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::app_state::AppState;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HealthResponse {
|
||||
status: &'static str,
|
||||
}
|
||||
|
||||
pub fn router<S>() -> Router<S>
|
||||
where
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new().route("/health", get(get_health))
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new().route("/", get(get_health))
|
||||
}
|
||||
|
||||
async fn get_health() -> Json<HealthResponse> {
|
||||
@ -1 +1,3 @@
|
||||
pub mod public_router;
|
||||
pub mod api_router;
|
||||
mod auth_router;
|
||||
mod health_router;
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@ -1,3 +1,4 @@
|
||||
mod app_state;
|
||||
mod config;
|
||||
mod db;
|
||||
mod error;
|
||||
@ -5,18 +6,12 @@ mod http;
|
||||
|
||||
use crate::db::database;
|
||||
use crate::error::AppError;
|
||||
use crate::http::public_router;
|
||||
use crate::http::api_router;
|
||||
use axum::Router;
|
||||
use tower_http::trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer};
|
||||
use tracing::{Level, info};
|
||||
use tracing_subscriber::{EnvFilter, fmt};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
#[allow(dead_code)]
|
||||
db: sqlx::PgPool,
|
||||
}
|
||||
|
||||
fn init_tracing() {
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| EnvFilter::new("info,tower_http=info,sqlx=warn"));
|
||||
@ -32,9 +27,9 @@ async fn main() -> Result<(), AppError> {
|
||||
let pool = database::create_pool(&cfg.database_url()).await?;
|
||||
info!("database connection established");
|
||||
|
||||
let state = AppState { db: pool };
|
||||
let state = app_state::AppState { db: pool };
|
||||
let app = Router::new()
|
||||
.merge(public_router::router())
|
||||
.nest("/api", api_router::router())
|
||||
.with_state(state)
|
||||
.layer(
|
||||
TraceLayer::new_for_http()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user