Embed docs into sync_server

This commit is contained in:
Andras Schmelczer 2025-12-07 19:23:13 +00:00
parent 3e7e1da7f5
commit 05b46a883f
11 changed files with 445 additions and 88 deletions

View file

@ -6,10 +6,10 @@ mod fetch_document_version;
mod fetch_document_version_content;
mod fetch_latest_document_version;
mod fetch_latest_documents;
mod index;
mod ping;
mod requests;
mod responses;
mod static_files;
mod update_document;
mod websocket;
@ -53,9 +53,10 @@ pub async fn create_server(config: Config) -> Result<()> {
let app = Router::new()
.nest("/", get_authed_routes(app_state.clone()))
.route("/", get(index::index))
.route("/", get(static_files::serve_index))
.route("/vaults/:vault_id/ping", get(ping::ping))
.route("/vaults/:vault_id/ws", get(websocket::websocket_handler))
.route("/*path", get(static_files::serve_static_file))
.layer(DefaultBodyLimit::disable())
.layer(RequestBodyLimitLayer::new(
app_state.config.server.max_body_size_mb * 1024 * 1024,

View file

@ -1,7 +0,0 @@
use axum::response::{Html, IntoResponse};
pub async fn index() -> impl IntoResponse {
const HTML_CONTENT: &str = include_str!("./assets/index.html");
let html_content = HTML_CONTENT;
Html(html_content)
}

View file

@ -0,0 +1,55 @@
use axum::{
extract::Path,
http::{StatusCode, header},
response::{Html, IntoResponse, Response},
};
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "../docs/.vitepress/dist"]
pub struct DocsAssets;
pub async fn serve_static_file(Path(path): Path<String>) -> Response {
let path = if path.is_empty() { "index.html" } else { &path };
match DocsAssets::get(path) {
Some(content) => {
let mime_type = mime_guess::from_path(path).first_or_octet_stream();
let body = content.data.into_owned();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime_type.as_ref())
.header(header::CACHE_CONTROL, "public, max-age=3600")
.body(body.into())
.unwrap()
}
None => {
// For SPA routing, if file not found, try serving index.html
match DocsAssets::get("index.html") {
Some(content) => {
Html(String::from_utf8_lossy(&content.data).to_string()).into_response()
}
None => (StatusCode::NOT_FOUND, "Documentation not found").into_response(),
}
}
}
}
pub async fn serve_index() -> Response {
match DocsAssets::get("index.html") {
Some(content) => Html(String::from_utf8_lossy(&content.data).to_string())
.into_response(),
None => {
Html(r"
<html>
<head><title>VaultLink Server</title></head>
<body>
<h1>VaultLink Sync Server</h1>
<p>Documentation not available. The server was compiled without embedded docs.</p>
</body>
</html>
").into_response()
}
}
}