2025-03-19 23:17:50 +07:00
|
|
|
use crate::{AppState, Env, RedisClient, SurrealClient};
|
|
|
|
|
use axum::{
|
|
|
|
|
http::{header, HeaderValue, Method},
|
|
|
|
|
Extension, Router,
|
|
|
|
|
};
|
|
|
|
|
use tower_http::cors::CorsLayer;
|
2025-03-19 20:55:29 +07:00
|
|
|
use utoipa_swagger_ui::SwaggerUi;
|
2025-02-07 22:26:32 +07:00
|
|
|
|
2025-02-03 21:53:42 +07:00
|
|
|
pub mod v1;
|
|
|
|
|
pub mod v2;
|
2025-02-07 22:26:32 +07:00
|
|
|
|
2025-02-25 02:00:19 +07:00
|
|
|
pub async fn apps(surrealdb: SurrealClient, redisdb: RedisClient) -> Router {
|
|
|
|
|
let state = AppState { surrealdb, redisdb };
|
2025-03-19 23:17:50 +07:00
|
|
|
let env = Env::new();
|
|
|
|
|
let cors_origins = match env.rust_env.as_str() {
|
|
|
|
|
"development" => vec!["http://localhost:5173"],
|
|
|
|
|
"production" => {
|
|
|
|
|
vec!["https://gacha.imphnen.dev", "https://imphnen.dev"]
|
|
|
|
|
}
|
|
|
|
|
_ => vec![
|
|
|
|
|
"http://localhost:5173",
|
|
|
|
|
"https://gacha.imphnen.dev",
|
|
|
|
|
"https://imphnen.dev",
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let allowed_origins: Vec<HeaderValue> = cors_origins
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|origin| origin.parse::<HeaderValue>().ok())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let cors_middleware = CorsLayer::new()
|
|
|
|
|
.allow_origin(allowed_origins)
|
|
|
|
|
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
|
|
|
|
|
.allow_headers([header::AUTHORIZATION, header::CONTENT_TYPE])
|
|
|
|
|
.allow_credentials(true);
|
|
|
|
|
|
2025-02-25 02:00:19 +07:00
|
|
|
Router::new()
|
|
|
|
|
.nest("/v1", v1::routes().await)
|
|
|
|
|
.nest("/v2", v2::routes().await)
|
2025-03-19 20:55:29 +07:00
|
|
|
.merge(SwaggerUi::new("/docs").url("/openapi.json", v1::docs_router()))
|
2025-03-19 23:17:50 +07:00
|
|
|
.layer(cors_middleware)
|
2025-02-25 02:00:19 +07:00
|
|
|
.layer(Extension(state))
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|