From 08a3d42f5152fef551a1490398b881b81a533e16 Mon Sep 17 00:00:00 2001 From: zeke Date: Tue, 26 Nov 2024 16:36:08 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=89=93=E5=8D=B0=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rs-capi/.dockerignore | 1 + rs-capi/src/main.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 rs-capi/.dockerignore diff --git a/rs-capi/.dockerignore b/rs-capi/.dockerignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/rs-capi/.dockerignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/rs-capi/src/main.rs b/rs-capi/src/main.rs index bb199f0..3e38249 100644 --- a/rs-capi/src/main.rs +++ b/rs-capi/src/main.rs @@ -1,3 +1,5 @@ +use axum::body::Body; +use axum::extract::Request; use axum::{ http::{HeaderMap, StatusCode}, response::{ @@ -7,15 +9,14 @@ use axum::{ routing::{get, post}, Json, Router, }; -use std::error::Error; -use tower_http::trace::TraceLayer; - use bytes::Bytes; use futures::{ channel::mpsc, stream::{Stream, StreamExt}, SinkExt, }; +use std::error::Error; +use tower_http::trace::TraceLayer; // use http::HeaderName as HttpHeaderName; use regex::Regex; use serde::Deserializer; @@ -259,8 +260,34 @@ async fn main() { // 处理聊天完成请求 async fn chat_completions( headers: HeaderMap, - Json(chat_request): Json, + request: Request, + // Json(chat_request): Json, ) -> Result { + // 提取并打印原始请求体 + const MAX_BODY_SIZE: usize = 20 * 1024 * 1024; + + let bytes = match axum::body::to_bytes(request.into_body(), MAX_BODY_SIZE).await { + Ok(bytes) => bytes, + Err(err) => { + tracing::error!("读取请求体失败: {}", err); + return Err(StatusCode::BAD_REQUEST); + } + }; + + // 打印原始请求体 + if let Ok(body_str) = String::from_utf8(bytes.to_vec()) { + tracing::info!("原始请求体: {}", body_str); + } + + // 尝试解析 JSON + let chat_request: ChatRequest = match serde_json::from_slice(&bytes) { + Ok(req) => req, + Err(err) => { + tracing::error!("JSON解析失败: {}", err); + return Err(StatusCode::BAD_REQUEST); + } + }; + // 验证认证 let auth_header = headers .get("authorization")