feat: auth functionality

This commit is contained in:
Maulana Sodiqin
2025-03-19 23:17:50 +07:00
parent 11d6249007
commit eb6cfd51ae
11 changed files with 384 additions and 35 deletions
+31 -2
View File
@@ -1,5 +1,9 @@
use crate::{AppState, RedisClient, SurrealClient};
use axum::{Extension, Router};
use crate::{AppState, Env, RedisClient, SurrealClient};
use axum::{
http::{header, HeaderValue, Method},
Extension, Router,
};
use tower_http::cors::CorsLayer;
use utoipa_swagger_ui::SwaggerUi;
pub mod v1;
@@ -7,9 +11,34 @@ pub mod v2;
pub async fn apps(surrealdb: SurrealClient, redisdb: RedisClient) -> Router {
let state = AppState { surrealdb, redisdb };
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);
Router::new()
.nest("/v1", v1::routes().await)
.nest("/v2", v2::routes().await)
.merge(SwaggerUi::new("/docs").url("/openapi.json", v1::docs_router()))
.layer(cors_middleware)
.layer(Extension(state))
}