tori/src/api/mod.rs
Fam Zheng 186d882f35 feat: JWT token generation API + auto-inject TORI_JWT in executor
- POST /tori/api/token — sign ES256 JWT with configurable private key
- exec.rs auto-generates and injects TORI_JWT env var for all commands
- Config: jwt_private_key field for PEM file path
2026-03-16 09:44:58 +00:00

243 lines
7.7 KiB
Rust

mod auth;
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()))
.merge(auth::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) => {
// Render markdown files as HTML
if full_path.extension().is_some_and(|e| e == "md") {
let md = String::from_utf8_lossy(&bytes);
let html = render_markdown_page(&md, &file_path);
return (
[(axum::http::header::CONTENT_TYPE, "text/html; charset=utf-8".to_string())],
html,
).into_response();
}
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(),
}
}
fn render_markdown_page(markdown: &str, title: &str) -> String {
use pulldown_cmark::{Parser, Options, html};
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
let parser = Parser::new_ext(markdown, opts);
let mut body = String::new();
html::push_html(&mut body, parser);
format!(r#"<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{title}</title>
<style>
:root {{
--text-primary: #1a1a2e;
--text-secondary: #6b7280;
--bg-primary: #ffffff;
--bg-secondary: #f7f8fa;
--border: #e2e5ea;
--accent: #2563eb;
--error: #dc2626;
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
margin: 0;
padding: 0;
}}
.page {{
max-width: 860px;
margin: 0 auto;
padding: 24px 32px;
min-height: 100vh;
}}
.toolbar {{
display: flex;
align-items: center;
gap: 16px;
margin-bottom: 24px;
padding-bottom: 12px;
border-bottom: 1px solid var(--border);
}}
.toolbar a {{
color: var(--accent);
text-decoration: none;
font-size: 14px;
font-weight: 500;
}}
.toolbar a:hover {{ text-decoration: underline; }}
.toolbar .title {{
font-size: 14px;
font-weight: 600;
color: var(--text-secondary);
letter-spacing: 0.5px;
}}
.body {{ line-height: 1.7; font-size: 15px; }}
.body h1 {{ font-size: 24px; font-weight: 700; margin: 0 0 16px; padding-bottom: 8px; border-bottom: 2px solid var(--border); }}
.body h2 {{ font-size: 20px; font-weight: 600; margin: 24px 0 12px; }}
.body h3 {{ font-size: 16px; font-weight: 600; margin: 20px 0 8px; }}
.body p {{ margin: 0 0 12px; }}
.body ul, .body ol {{ margin: 0 0 12px; padding-left: 24px; }}
.body li {{ margin-bottom: 4px; }}
.body pre {{
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: 6px;
padding: 12px 16px;
overflow-x: auto;
margin: 0 0 12px;
font-size: 13px;
line-height: 1.5;
}}
.body code {{ font-family: 'JetBrains Mono', 'Fira Code', monospace; font-size: 13px; }}
.body :not(pre) > code {{ background: var(--bg-secondary); padding: 2px 6px; border-radius: 4px; }}
.body table {{ width: 100%; border-collapse: collapse; margin: 0 0 12px; font-size: 14px; }}
.body th, .body td {{ border: 1px solid var(--border); padding: 8px 12px; text-align: left; }}
.body th {{ background: var(--bg-secondary); font-weight: 600; }}
.body blockquote {{ border-left: 3px solid var(--accent); padding-left: 16px; margin: 0 0 12px; color: var(--text-secondary); }}
.body a {{ color: var(--accent); text-decoration: none; }}
.body a:hover {{ text-decoration: underline; }}
.body img {{ max-width: 100%; border-radius: 6px; }}
.body hr {{ border: none; border-top: 1px solid var(--border); margin: 20px 0; }}
.body input[type="checkbox"] {{ margin-right: 6px; }}
</style>
</head>
<body>
<div class="page">
<div class="toolbar">
<a href="javascript:history.back()">&larr; 返回</a>
<span class="title">{title}</span>
</div>
<div class="body">{body}</div>
</div>
</body>
</html>"#, title = title, body = body)
}