From f128d5f6c5b2841e23f710bd7821ea4ffc89c62e Mon Sep 17 00:00:00 2001 From: "benjamin.747" Date: Tue, 26 Aug 2025 15:11:54 +0800 Subject: [PATCH] feat: add cors config for orion --- orion-server/.env | 3 ++- orion-server/Cargo.toml | 8 +++++--- orion-server/src/server.rs | 38 +++++++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/orion-server/.env b/orion-server/.env index 0f281d582..8b79d6b16 100644 --- a/orion-server/.env +++ b/orion-server/.env @@ -1,4 +1,5 @@ BUILD_LOG_DIR="/tmp/megadir/buck2ctl" DATABASE_URL="postgres://postgres:postgres@localhost/orion" PORT=80 -MONOBASE_URL="http://git.gitmega.com" \ No newline at end of file +MONOBASE_URL="http://git.gitmega.com" +ALLOWED_CORS_ORIGINS = "http://local.gitmega.com, https://app.gitmega.com, http://app.gitmono.test" \ No newline at end of file diff --git a/orion-server/Cargo.toml b/orion-server/Cargo.toml index 965830b1b..8f6913afe 100644 --- a/orion-server/Cargo.toml +++ b/orion-server/Cargo.toml @@ -4,8 +4,9 @@ version = "0.1.0" edition = "2024" [dependencies] -orion = { workspace = true} +orion = { workspace = true } +http = { workspace = true } axum = { workspace = true, features = ["macros", "ws"] } axum-extra = { workspace = true, features = ["erased-json"] } tokio = { workspace = true, features = ["rt-multi-thread", "fs", "process"] } @@ -16,7 +17,8 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } futures-util = { workspace = true } uuid = { workspace = true, features = ["v7"] } -tower-http = { workspace = true, features = ["trace"] } +tower-http = { workspace = true, features = ["trace", "cors"] } +tower = { workspace = true } sea-orm = { workspace = true, features = [ "sqlx-postgres", "runtime-tokio-rustls", @@ -35,4 +37,4 @@ reqwest.workspace = true thiserror = "2.0" [dev-dependencies] -tempfile = "3" \ No newline at end of file +tempfile = "3" diff --git a/orion-server/src/server.rs b/orion-server/src/server.rs index cbe4f6938..c04a7c9a3 100644 --- a/orion-server/src/server.rs +++ b/orion-server/src/server.rs @@ -1,17 +1,21 @@ -use crate::api::{self, AppState}; -use crate::model::builds; +use std::net::SocketAddr; +use std::time::Duration; + use axum::Router; use axum::routing::get; +use http::{HeaderValue, Method}; use sea_orm::{ ActiveValue::Set, ColumnTrait, ConnectionTrait, Database, DatabaseConnection, DbErr, EntityTrait, QueryFilter, Schema, TransactionTrait, }; -use std::net::SocketAddr; -use std::time::Duration; +use tower::ServiceBuilder; +use tower_http::cors::CorsLayer; use tower_http::trace::TraceLayer; use utoipa::OpenApi; use utoipa_swagger_ui::SwaggerUi; +use crate::api::{self, AppState}; +use crate::model::builds; /// OpenAPI documentation configuration #[derive(OpenApi)] #[openapi( @@ -57,12 +61,36 @@ pub async fn start_server(port: u16) { // Start queue manager tokio::spawn(api::start_queue_manager(state.clone())); + let origins: Vec = std::env::var("ALLOWED_CORS_ORIGINS") + .unwrap() + .split(',') + .map(|x| x.trim().parse::().unwrap()) + .collect(); + let app = Router::new() .route("/", get(|| async { "Hello, World!" })) .merge(api::routers()) .merge(SwaggerUi::new("/swagger-ui").url("/api-doc/openapi.json", ApiDoc::openapi())) .with_state(state) - .layer(TraceLayer::new_for_http()); + .layer(TraceLayer::new_for_http()) + .layer( + ServiceBuilder::new().layer( + CorsLayer::new() + .allow_origin(origins) + .allow_headers(vec![ + http::header::AUTHORIZATION, + http::header::CONTENT_TYPE, + ]) + .allow_methods([ + Method::GET, + Method::POST, + Method::OPTIONS, + Method::DELETE, + Method::PUT, + ]) + .allow_credentials(true), + ), + ); tracing::info!("Listening on port {}", port); let addr = tokio::net::TcpListener::bind(&format!("0.0.0.0:{port}"))