refactor(core): move doc handler to core and split API.md per app

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-04-20 08:18:51 +08:00
parent 4761e88c81
commit b651b6af66
8 changed files with 54 additions and 33 deletions

View File

@ -1,40 +1,10 @@
use axum::{
http::{header, HeaderMap, HeaderValue, StatusCode},
response::IntoResponse,
};
use axum::response::IntoResponse;
use plc_platform_core::util::response::ApiErr;
pub async fn get_api_md() -> Result<impl IntoResponse, ApiErr> {
let content = tokio::fs::read_to_string("API.md")
.await
.map_err(|err| {
tracing::error!("Failed to read API.md: {}", err);
ApiErr::NotFound("API.md not found".to_string(), None)
})?;
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/markdown; charset=utf-8"),
);
Ok((StatusCode::OK, headers, content))
plc_platform_core::handler::doc::serve_markdown("docs/api-feeder.md").await
}
pub async fn get_readme_md() -> Result<impl IntoResponse, ApiErr> {
let content = tokio::fs::read_to_string("README.md")
.await
.map_err(|err| {
tracing::error!("Failed to read README.md: {}", err);
ApiErr::NotFound("README.md not found".to_string(), None)
})?;
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/markdown; charset=utf-8"),
);
Ok((StatusCode::OK, headers, content))
plc_platform_core::handler::doc::serve_markdown("README.md").await
}

View File

@ -0,0 +1 @@
pub mod doc;

View File

@ -0,0 +1,10 @@
use axum::response::IntoResponse;
use plc_platform_core::util::response::ApiErr;
pub async fn get_api_md() -> Result<impl IntoResponse, ApiErr> {
plc_platform_core::handler::doc::serve_markdown("docs/api-ops.md").await
}
pub async fn get_readme_md() -> Result<impl IntoResponse, ApiErr> {
plc_platform_core::handler::doc::serve_markdown("README.md").await
}

View File

@ -1,4 +1,5 @@
pub mod app;
pub mod handler;
pub mod router;
pub use app::{run, test_state, AppState};

View File

@ -1 +1,2 @@
pub mod doc;
pub mod log;

View File

@ -0,0 +1,23 @@
use axum::{
http::{header, HeaderMap, HeaderValue, StatusCode},
response::IntoResponse,
};
use crate::util::response::ApiErr;
pub async fn serve_markdown(path: &str) -> Result<impl IntoResponse, ApiErr> {
let content = tokio::fs::read_to_string(path)
.await
.map_err(|err| {
tracing::error!("Failed to read {}: {}", path, err);
ApiErr::NotFound(format!("{} not found", path), None)
})?;
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/markdown; charset=utf-8"),
);
Ok((StatusCode::OK, headers, content))
}

15
docs/api-ops.md Normal file
View File

@ -0,0 +1,15 @@
# 运转系统 API
## 健康检查
- `GET /api/health` — 返回应用名称和状态
## 日志
- `GET /api/logs` — 拉取日志内容
- `GET /api/logs/stream` — SSE 增量推送
## 文档
- `GET /api/docs/api-md` — 获取 API 文档
- `GET /api/docs/readme-md` — 获取 README