Files
imphnen-backend-service/src/apps/mod.rs
T

47 lines
1.2 KiB
Rust
Raw Normal View History

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-03-20 22:41:20 +07:00
pub use v1::*;
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() {
2025-03-20 17:07:38 +07:00
"development" => vec!["http://localhost:3000"],
2025-03-19 23:17:50 +07:00
"production" => {
vec!["https://gacha.imphnen.dev", "https://imphnen.dev"]
}
_ => vec![
2025-03-20 17:07:38 +07:00
"http://localhost:3000",
2025-03-19 23:17:50 +07:00
"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
}