Răsfoiți Sursa

【新增】添加 CORS 支持

- 引入 actix-cors 依赖,实现跨域请求支持
- 升级 yaa 包版本到 2.7.0
- 在 HttpServer 中配置 CORS 中间件,允许所有来源和常用 HTTP 方法
PJ568 1 an în urmă
părinte
comite
7f6acbf710
3 a modificat fișierele cu 33 adăugiri și 2 ștergeri
  1. 18 1
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 13 0
      src/main.rs

+ 18 - 1
Cargo.lock

@@ -19,6 +19,21 @@ dependencies = [
  "tracing",
 ]
 
+[[package]]
+name = "actix-cors"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "daa239b93927be1ff123eebada5a3ff23e89f0124ccb8609234e5103d5a5ae6d"
+dependencies = [
+ "actix-utils",
+ "actix-web",
+ "derive_more",
+ "futures-util",
+ "log",
+ "once_cell",
+ "smallvec",
+]
+
 [[package]]
 name = "actix-http"
 version = "3.10.0"
@@ -694,6 +709,7 @@ dependencies = [
  "futures-task",
  "pin-project-lite",
  "pin-utils",
+ "slab",
 ]
 
 [[package]]
@@ -2433,8 +2449,9 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
 
 [[package]]
 name = "yaa"
-version = "2.6.5"
+version = "2.7.0"
 dependencies = [
+ "actix-cors",
  "actix-web",
  "anyhow",
  "async-trait",

+ 2 - 1
Cargo.toml

@@ -1,9 +1,10 @@
 [package]
 name = "yaa"
-version = "2.6.5"
+version = "2.7.0"
 edition = "2024"
 
 [dependencies]
+actix-cors = "0.7.1"
 actix-web = "4.10.2"
 anyhow = "1.0.80"
 async-trait = "0.1.88"

+ 13 - 0
src/main.rs

@@ -1,6 +1,7 @@
 mod agent;
 mod cli;
 mod core;
+use actix_cors::Cors;
 use actix_web::{App, HttpServer, web};
 use agent::tools::finish::FinishTool;
 use agent::tools::rethink::RethinkTool;
@@ -189,7 +190,19 @@ async fn main() -> std::io::Result<()> {
     if args.serve {
         println!("启动服务监听模式,端口: {}", args.port);
         HttpServer::new(move || {
+            // 配置 CORS 中间件
+            let cors = Cors::default()
+                .allow_any_origin()  // 允许所有来源
+                .allowed_methods(vec!["GET", "POST", "PUT", "DELETE"])  // 允许的方法
+                .allowed_headers(vec![
+                    actix_web::http::header::AUTHORIZATION,
+                    actix_web::http::header::ACCEPT,
+                    actix_web::http::header::CONTENT_TYPE,
+                ])  // 允许的请求头
+                .max_age(3600);  // 预检请求缓存时间
+
             App::new()
+                .wrap(cors)  // 应用 CORS 中间件
                 .app_data(web::Data::new(session_data.clone()))
                 .service(web::resource("/").route(web::post().to(handle_request)))
         })