Skip to main content

Crate docgen_server

Crate docgen_server 

Source
Expand description

docgen-server is the dev-only server behind docgen dev: an axum app (bound to 127.0.0.1 only) that serves the built site, watches docs/ for changes (debounced), rebuilds via docgen_build::build_site, pushes a live-reload signal over SSE, and exposes a path-guarded markdown write endpoint for the in-browser editor.

Nothing in this crate ships in a static docgen build dist: the editor UI, the reload client, the write/SSE endpoints, and the vendored CodeMirror assets exist ONLY while this server runs.

Structs§

AppState
Shared, cheaply-clonable state behind every handler. Clone bumps the Arc/broadcast handle.
DevOptions
Dev-server configuration.

Enums§

PathGuardError
Errors from resolve_doc_path; each maps to an HTTP status (see handlers).
ReloadEvent
One live-reload signal. Carried over the SSE channel.

Functions§

dev_bind_addr
The loopback bind address for the dev server. NEVER 0.0.0.0 — the dev server (editor + write endpoint) must not be reachable off-host.
inject_dev_html
Post-process a served HTML body: inject the reload-client script + editor toggle + editor island scripts/styles immediately before </body>. Dev-only; never run by docgen build. Pure string fn so it is unit-testable.
rebuild_and_reload
Rebuild the site into state.out_dir (Dev mode + dev-asset emission), then broadcast a reload. Called on every debounced fs change AND after a successful editor write. Returns Err only on a hard build failure; the caller logs and keeps serving the last good build. This guarantee holds because docgen_build::build_site is atomic — it stages into a temp dir and only swaps out_dir on full success, so a failed rebuild leaves the served dir (the previous good build) untouched rather than torn down.
resolve_doc_path
Resolve a client-supplied doc-relative path (e.g. "guide/intro.md") to a canonical absolute path strictly inside docs_dir, or reject. docs_dir MUST already be canonicalized by the caller. Layered checks mirror the original validateRepoDocPath:
router
Build the axum router (NO listener) for the given state. Split out so handler tests can oneshot requests without binding a port.
serve
Run the dev server: initial build, spawn the debounced watcher, bind 127.0.0.1, serve until Ctrl-C. Blocking entry point the docgen dev CLI arm calls. Owns its own tokio runtime so the docgen bin’s main stays a plain fn main() -> Result<()>.