mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 18:37:46 +08:00
refactor: 拆分 gateway 单体为独立 crate,新增 systemd 部署方案
将 gateway 内部的 model-fetch、provider-transport、scheduler-core、 usage-runtime、video-tasks-core 模块提取为独立 crate;重构 gateway 内部模块结构(state/router/cache/data/query 等);移除大量遗留模块 文件;新增 systemd 二进制部署骨架及相关文档;更新前端 usage 相关 API 和组件。
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use aether_data::repository::video_tasks::{
|
||||
InMemoryVideoTaskRepository, UpsertVideoTask, VideoTaskWriteRepository,
|
||||
};
|
||||
use axum::body::Body;
|
||||
use axum::routing::any;
|
||||
use axum::{extract::Request, Json, Router};
|
||||
use http::StatusCode;
|
||||
use serde_json::json;
|
||||
|
||||
use super::{build_router_with_state, build_state_with_execution_runtime_override, start_server};
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_reads_openai_video_task_via_data_read_side_without_hitting_public_route() {
|
||||
let public_hits = Arc::new(Mutex::new(0usize));
|
||||
let public_hits_clone = Arc::clone(&public_hits);
|
||||
|
||||
let upstream = Router::new()
|
||||
.route(
|
||||
"/api/internal/gateway/resolve",
|
||||
any(|_request: Request| async move {
|
||||
Json(json!({
|
||||
"action": "proxy_public",
|
||||
"route_class": "ai_public",
|
||||
"route_family": "openai",
|
||||
"route_kind": "video",
|
||||
"auth_endpoint_signature": "openai:video",
|
||||
"execution_runtime_candidate": true,
|
||||
"auth_context": {
|
||||
"user_id": "user-video-db-123",
|
||||
"api_key_id": "key-video-db-123",
|
||||
"access_allowed": true
|
||||
},
|
||||
"public_path": "/v1/videos/task-db-123"
|
||||
}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/v1/videos/task-db-123",
|
||||
any(move |_request: Request| {
|
||||
let public_hits_inner = Arc::clone(&public_hits_clone);
|
||||
async move {
|
||||
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::IM_A_TEAPOT, Body::from("public-route-hit"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let repository = Arc::new(InMemoryVideoTaskRepository::default());
|
||||
repository
|
||||
.upsert(UpsertVideoTask {
|
||||
id: "task-db-123".to_string(),
|
||||
short_id: Some("short-task-db-123".to_string()),
|
||||
request_id: "request-db-123".to_string(),
|
||||
user_id: Some("user-video-db-123".to_string()),
|
||||
api_key_id: Some("api-key-video-db-123".to_string()),
|
||||
username: Some("video-user".to_string()),
|
||||
api_key_name: Some("video-key".to_string()),
|
||||
external_task_id: Some("ext-video-db-123".to_string()),
|
||||
provider_id: Some("provider-video-db-123".to_string()),
|
||||
endpoint_id: Some("endpoint-video-db-123".to_string()),
|
||||
key_id: Some("provider-key-video-db-123".to_string()),
|
||||
client_api_format: Some("openai:video".to_string()),
|
||||
provider_api_format: Some("openai:video".to_string()),
|
||||
format_converted: false,
|
||||
model: Some("sora-2".to_string()),
|
||||
prompt: Some("hello from db".to_string()),
|
||||
original_request_body: Some(json!({"prompt": "hello from db"})),
|
||||
duration_seconds: Some(4),
|
||||
resolution: Some("720p".to_string()),
|
||||
aspect_ratio: Some("16:9".to_string()),
|
||||
size: Some("1280x720".to_string()),
|
||||
status: aether_data::repository::video_tasks::VideoTaskStatus::Processing,
|
||||
progress_percent: 45,
|
||||
progress_message: Some("working".to_string()),
|
||||
retry_count: 0,
|
||||
poll_interval_seconds: 10,
|
||||
next_poll_at_unix_secs: Some(124),
|
||||
poll_count: 1,
|
||||
max_poll_count: 360,
|
||||
created_at_unix_secs: 123,
|
||||
submitted_at_unix_secs: Some(123),
|
||||
completed_at_unix_secs: None,
|
||||
updated_at_unix_secs: 124,
|
||||
error_code: None,
|
||||
error_message: None,
|
||||
video_url: None,
|
||||
request_metadata: None,
|
||||
})
|
||||
.await
|
||||
.expect("upsert should succeed");
|
||||
|
||||
let gateway = build_router_with_state(
|
||||
build_state_with_execution_runtime_override(upstream_url.clone())
|
||||
.with_video_task_data_reader_for_tests(repository),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!("{gateway_url}/v1/videos/task-db-123"))
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body: serde_json::Value = response.json().await.expect("body should parse");
|
||||
assert_eq!(body["id"], "task-db-123");
|
||||
assert_eq!(body["status"], "processing");
|
||||
assert_eq!(body["progress"], 45);
|
||||
assert_eq!(body["created_at"], 123);
|
||||
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_reads_gemini_video_task_via_data_read_side_without_hitting_public_route() {
|
||||
let public_hits = Arc::new(Mutex::new(0usize));
|
||||
let public_hits_clone = Arc::clone(&public_hits);
|
||||
|
||||
let upstream = Router::new()
|
||||
.route(
|
||||
"/api/internal/gateway/resolve",
|
||||
any(|_request: Request| async move {
|
||||
Json(json!({
|
||||
"action": "proxy_public",
|
||||
"route_class": "ai_public",
|
||||
"route_family": "gemini",
|
||||
"route_kind": "video",
|
||||
"auth_endpoint_signature": "gemini:video",
|
||||
"execution_runtime_candidate": true,
|
||||
"auth_context": {
|
||||
"user_id": "user-video-db-123",
|
||||
"api_key_id": "key-video-db-123",
|
||||
"access_allowed": true
|
||||
},
|
||||
"public_path": "/v1beta/models/veo-3/operations/localshort123"
|
||||
}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/v1beta/models/veo-3/operations/localshort123",
|
||||
any(move |_request: Request| {
|
||||
let public_hits_inner = Arc::clone(&public_hits_clone);
|
||||
async move {
|
||||
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::IM_A_TEAPOT, Body::from("public-route-hit"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let repository = Arc::new(InMemoryVideoTaskRepository::default());
|
||||
repository
|
||||
.upsert(UpsertVideoTask {
|
||||
id: "task-db-456".to_string(),
|
||||
short_id: Some("localshort123".to_string()),
|
||||
request_id: "request-db-456".to_string(),
|
||||
user_id: Some("user-video-db-123".to_string()),
|
||||
api_key_id: Some("api-key-video-db-123".to_string()),
|
||||
username: Some("video-user".to_string()),
|
||||
api_key_name: Some("video-key".to_string()),
|
||||
external_task_id: Some("operations/ext-video-db-123".to_string()),
|
||||
provider_id: Some("provider-video-db-123".to_string()),
|
||||
endpoint_id: Some("endpoint-video-db-123".to_string()),
|
||||
key_id: Some("provider-key-video-db-123".to_string()),
|
||||
client_api_format: Some("gemini:video".to_string()),
|
||||
provider_api_format: Some("gemini:video".to_string()),
|
||||
format_converted: false,
|
||||
model: Some("veo-3".to_string()),
|
||||
prompt: Some("hello from gemini db".to_string()),
|
||||
original_request_body: Some(json!({"prompt": "hello from gemini db"})),
|
||||
duration_seconds: Some(8),
|
||||
resolution: Some("720p".to_string()),
|
||||
aspect_ratio: Some("16:9".to_string()),
|
||||
size: Some("720p".to_string()),
|
||||
status: aether_data::repository::video_tasks::VideoTaskStatus::Completed,
|
||||
progress_percent: 100,
|
||||
progress_message: None,
|
||||
retry_count: 0,
|
||||
poll_interval_seconds: 10,
|
||||
next_poll_at_unix_secs: None,
|
||||
poll_count: 3,
|
||||
max_poll_count: 360,
|
||||
created_at_unix_secs: 223,
|
||||
submitted_at_unix_secs: Some(223),
|
||||
completed_at_unix_secs: Some(224),
|
||||
updated_at_unix_secs: 224,
|
||||
error_code: None,
|
||||
error_message: None,
|
||||
video_url: None,
|
||||
request_metadata: Some(json!({
|
||||
"rust_local_snapshot": {
|
||||
"metadata": {
|
||||
"generateVideoResponse": {
|
||||
"generatedSamples": [
|
||||
{
|
||||
"video": {
|
||||
"uri": "/v1beta/files/aev_localshort123:download?alt=media"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})),
|
||||
})
|
||||
.await
|
||||
.expect("upsert should succeed");
|
||||
|
||||
let gateway = build_router_with_state(
|
||||
build_state_with_execution_runtime_override(upstream_url.clone())
|
||||
.with_video_task_data_reader_for_tests(repository),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/v1beta/models/veo-3/operations/localshort123"
|
||||
))
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body: serde_json::Value = response.json().await.expect("body should parse");
|
||||
assert_eq!(body["name"], "models/veo-3/operations/localshort123");
|
||||
assert_eq!(body["done"], true);
|
||||
assert_eq!(
|
||||
body["response"]["generateVideoResponse"]["generatedSamples"][0]["video"]["uri"],
|
||||
"/v1beta/files/aev_localshort123:download?alt=media"
|
||||
);
|
||||
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
Reference in New Issue
Block a user