use anyhow::Result;
use axum::{
body::{Body, Bytes},
http::{header, HeaderValue, Method, Request, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::Mutex;
use tower_http::cors::{Any, CorsLayer};
#[derive(Clone)]
pub struct AppState {
pub start_time: std::time::Instant,
}
impl Default for AppState {
fn default() -> Self {
Self {
start_time: std::time::Instant::now(),
}
}
}
#[derive(Clone)]
pub struct FastApiConfig {
pub workers: usize,
pub enable_cors: bool,
pub enable_swagger: bool,
pub debug: bool,
pub max_request_size: usize,
pub timeout_seconds: u64,
pub cors_origins: Vec<String>,
pub api_prefix: String,
pub docs_url: String,
pub redoc_url: String,
pub openapi_url: String,
}
impl Default for FastApiConfig {
fn default() -> Self {
Self {
workers: 1,
enable_cors: true,
enable_swagger: true,
debug: false,
max_request_size: 1024 * 1024,
timeout_seconds: 30,
cors_origins: vec!["*".into()],
api_prefix: "/api".into(),
docs_url: "/docs".into(),
redoc_url: "/redoc".into(),
openapi_url: "/openapi.json".into(),
}
}
}
pub fn create_router() -> axum::Router {
use axum::{routing::get, Router};
Router::new().route("/health", get(|| async { "healthy" }))
}
pub fn build_router() -> axum::Router {
create_router()
}
pub struct AppBuilder {
pub state: Option<AppState>,
pub config: FastApiConfig,
}
impl AppBuilder {
pub async fn build(self) -> anyhow::Result<(AppState, axum::Router)> {
let state = self.state.unwrap_or_default();
let router = create_router();
Ok((state, router))
}
}