- Rewrite template.rs to scan all remote branches via git commands (git fetch/branch -r/ls-tree/git show/git archive) - Add manual template picker dropdown in CreateForm UI - Remove sentence-transformers/embed.py from Dockerfile (separate container) - Clean up Gitea API approach, use local git repo instead - Add chat panel and sidebar layout improvements
130 lines
4.0 KiB
Rust
130 lines
4.0 KiB
Rust
mod chat;
|
|
mod kb;
|
|
pub mod obj;
|
|
mod projects;
|
|
mod settings;
|
|
mod timers;
|
|
mod workflows;
|
|
|
|
use std::sync::Arc;
|
|
use axum::{
|
|
body::Body,
|
|
extract::{Path, State, Request},
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
routing::{get, any},
|
|
Json, Router,
|
|
};
|
|
|
|
use crate::AppState;
|
|
|
|
pub(crate) type ApiResult<T> = Result<Json<T>, Response>;
|
|
|
|
pub(crate) fn db_err(e: sqlx::Error) -> Response {
|
|
tracing::error!("Database error: {}", e);
|
|
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
|
|
}
|
|
|
|
pub fn router(state: Arc<AppState>) -> Router {
|
|
Router::new()
|
|
.merge(projects::router(state.clone()))
|
|
.merge(workflows::router(state.clone()))
|
|
.merge(timers::router(state.clone()))
|
|
.merge(kb::router(state.clone()))
|
|
.merge(settings::router(state.clone()))
|
|
.merge(chat::router(state.clone()))
|
|
.route("/projects/{id}/files/{*path}", get(serve_project_file))
|
|
.route("/projects/{id}/app/{*path}", any(proxy_to_service).with_state(state.clone()))
|
|
.route("/projects/{id}/app/", any(proxy_to_service_root).with_state(state))
|
|
}
|
|
|
|
async fn proxy_to_service_root(
|
|
State(state): State<Arc<AppState>>,
|
|
Path(project_id): Path<String>,
|
|
req: Request<Body>,
|
|
) -> Response {
|
|
proxy_impl(&state, &project_id, "/", req).await
|
|
}
|
|
|
|
async fn proxy_to_service(
|
|
State(state): State<Arc<AppState>>,
|
|
Path((project_id, path)): Path<(String, String)>,
|
|
req: Request<Body>,
|
|
) -> Response {
|
|
proxy_impl(&state, &project_id, &format!("/{}", path), req).await
|
|
}
|
|
|
|
async fn proxy_impl(
|
|
state: &AppState,
|
|
project_id: &str,
|
|
path: &str,
|
|
req: Request<Body>,
|
|
) -> Response {
|
|
let port = match state.agent_mgr.get_service_port(project_id).await {
|
|
Some(p) => p,
|
|
None => return (StatusCode::SERVICE_UNAVAILABLE, "服务未启动").into_response(),
|
|
};
|
|
|
|
let query = req.uri().query().map(|q| format!("?{}", q)).unwrap_or_default();
|
|
let url = format!("http://127.0.0.1:{}{}{}", port, path, query);
|
|
|
|
let client = reqwest::Client::new();
|
|
let method = req.method().clone();
|
|
let headers = req.headers().clone();
|
|
let body_bytes = match axum::body::to_bytes(req.into_body(), 10 * 1024 * 1024).await {
|
|
Ok(b) => b,
|
|
Err(_) => return (StatusCode::BAD_REQUEST, "请求体过大").into_response(),
|
|
};
|
|
|
|
let mut upstream_req = client.request(method, &url);
|
|
for (key, val) in headers.iter() {
|
|
if key != "host" {
|
|
upstream_req = upstream_req.header(key, val);
|
|
}
|
|
}
|
|
upstream_req = upstream_req.body(body_bytes);
|
|
|
|
match upstream_req.send().await {
|
|
Ok(resp) => {
|
|
let status = StatusCode::from_u16(resp.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
|
|
let resp_headers = resp.headers().clone();
|
|
let body = resp.bytes().await.unwrap_or_default();
|
|
let mut response = (status, body).into_response();
|
|
for (key, val) in resp_headers.iter() {
|
|
if let Ok(name) = axum::http::header::HeaderName::from_bytes(key.as_ref()) {
|
|
response.headers_mut().insert(name, val.clone());
|
|
}
|
|
}
|
|
response
|
|
}
|
|
Err(_) => (StatusCode::BAD_GATEWAY, "无法连接到后端服务").into_response(),
|
|
}
|
|
}
|
|
|
|
async fn serve_project_file(
|
|
Path((project_id, file_path)): Path<(String, String)>,
|
|
) -> Response {
|
|
let full_path = std::path::PathBuf::from("/app/data/workspaces")
|
|
.join(&project_id)
|
|
.join(&file_path);
|
|
|
|
// Prevent path traversal
|
|
if file_path.contains("..") {
|
|
return (StatusCode::BAD_REQUEST, "Invalid path").into_response();
|
|
}
|
|
|
|
match tokio::fs::read(&full_path).await {
|
|
Ok(bytes) => {
|
|
let mime = mime_guess::from_path(&full_path)
|
|
.first_or_octet_stream()
|
|
.to_string();
|
|
(
|
|
[(axum::http::header::CONTENT_TYPE, mime)],
|
|
bytes,
|
|
).into_response()
|
|
}
|
|
Err(_) => (StatusCode::NOT_FOUND, "File not found").into_response(),
|
|
}
|
|
}
|
|
|