refactor: modularize server.rs into separate route modules
- Extract scan.rs, files.rs, types.rs, processing.rs, visual_chunk_search.rs - Move AppState and AppConfig to types.rs - Each module exposes pub fn xxx_routes() -> Router<AppState> - server.rs reduced from 5005 to 118 lines (orchestrator only) - All stubs filled with real implementations from git history - Verify: cargo check, clippy, tests all pass
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
use axum::{extract::Path, http::StatusCode, routing::get, Router};
|
||||
|
||||
use super::types::AppState;
|
||||
|
||||
async fn doc_redirect() -> axum::response::Redirect {
|
||||
axum::response::Redirect::to("/doc-wasm")
|
||||
}
|
||||
|
||||
async fn wasm_doc_handler() -> Result<impl axum::response::IntoResponse, (StatusCode, &'static str)>
|
||||
{
|
||||
let path =
|
||||
std::path::Path::new("/Users/accusys/momentry_core_0.1/docs_v1.0/doc_wasm/index.html");
|
||||
match tokio::fs::read_to_string(path).await {
|
||||
Ok(html) => Ok(([("content-type", "text/html; charset=utf-8")], html)),
|
||||
Err(_) => Err((StatusCode::NOT_FOUND, "Doc not found")),
|
||||
}
|
||||
}
|
||||
|
||||
async fn wasm_doc_file_handler(
|
||||
Path(file): Path<String>,
|
||||
) -> Result<impl axum::response::IntoResponse, (StatusCode, &'static str)> {
|
||||
if file.contains("..") || file.contains("//") {
|
||||
return Err((StatusCode::NOT_FOUND, "Invalid path"));
|
||||
}
|
||||
let base = std::path::Path::new("/Users/accusys/momentry_core_0.1/docs_v1.0/doc_wasm");
|
||||
let path = base.join(&file);
|
||||
if !path.exists() || !path.starts_with(base) {
|
||||
return Err((StatusCode::NOT_FOUND, "File not found"));
|
||||
}
|
||||
let data = tokio::fs::read(&path)
|
||||
.await
|
||||
.map_err(|_| (StatusCode::NOT_FOUND, "Read error"))?;
|
||||
let mime = if file.ends_with(".wasm") {
|
||||
"application/wasm"
|
||||
} else if file.ends_with(".js") {
|
||||
"application/javascript"
|
||||
} else if file.ends_with(".md") {
|
||||
"text/markdown; charset=utf-8"
|
||||
} else if file.ends_with(".css") {
|
||||
"text/css"
|
||||
} else {
|
||||
"application/octet-stream"
|
||||
};
|
||||
Ok(([("content-type", mime)], data))
|
||||
}
|
||||
|
||||
pub fn doc_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/doc", get(doc_redirect))
|
||||
.route("/doc/*file", get(doc_redirect))
|
||||
.route("/dev-doc", get(doc_redirect))
|
||||
.route("/doc-wasm", get(wasm_doc_handler))
|
||||
.route("/doc-wasm/*file", get(wasm_doc_file_handler))
|
||||
}
|
||||
Reference in New Issue
Block a user