Files
plainleaf/server/src/state.rs
T
Zef HemelandGitHub 91e76d0c78 Rust backend/CLI replacement (previously Go) (#2010)
* Rust backend: initial setup and space primitives implementation + tests

* Rust: initial basic server setup

* Rust server: auth layer

* Rust: added specific auth layers

* Rust: shell and proxy endpoints

* Rust: SSR and Prometheus metrics

* Rust: runtime API

* Rust: basic working silverbullet binary server

* Rust: fix client file caching

* Rust: in dev mode, update version number dynamically

* Rust: build-rs Makefile target

* Rust: auth and start of 'sb' CLI

* Rust: headless Chrome runtime runner

* Rust: basic e2e tests for Rust build

* Rust: docker builds

* Rust: forgot to commit silverbullet-bin code

* Rust: cross compile and chrome driver fixes

* Rust: waiting for ready status fixes

* Rust: sb-CLI port

* Rust: use native build toolchain

* Rust: fix build chain

* Rust: edge release builds

* Rust: FreeBSD and armv7 builds

* Rust: edge-upgrade from edge-rust release

* Rust: fix FreeBSD build

* Rust: fix FreeBSD build

* Rust: get rid of Go code

* refactor: rename library crates to server- prefix convention

* Rust: refactoring, moving crypto around

* Rust: more refactoring

* Runtime: internal renaming

* Fix: cursor placement after paste on Webkit browsers

* Tweak how version numbers are generated and consumed

* Cleanup CI situation

* Fix CI

* Fix e2e

* ci: website image uses :edge channel (was :v2) post-merge

* Rust: restore manifest.json endpoint

* Rust: ready to merge
2026-06-16 08:40:41 +02:00

86 lines
3.4 KiB
Rust

use std::sync::Arc;
use silverbullet_server_common::{BootConfig, SpacePrimitives};
use crate::auth::RequestAuthorizer;
use crate::metrics::Metrics;
use crate::runtime::RuntimeBackend;
use crate::shell::ShellConfig;
/// The version string reported at `/.ping` (`X-Server-Version`). Normally
/// `Static`. The standalone binary uses `Dynamic` in debug builds so the
/// reported version follows a live-rebuilt client bundle (served from disk)
/// without a server restart.
pub enum ServerVersion {
Static(String),
Dynamic(Box<dyn Fn() -> String + Send + Sync>),
}
impl ServerVersion {
/// The current version string.
pub fn get(&self) -> String {
match self {
ServerVersion::Static(v) => v.clone(),
ServerVersion::Dynamic(f) => f(),
}
}
}
impl From<String> for ServerVersion {
fn from(v: String) -> Self {
ServerVersion::Static(v)
}
}
impl From<&str> for ServerVersion {
fn from(v: &str) -> Self {
ServerVersion::Static(v.to_string())
}
}
/// Shared state for the HTTP server. Holds what the file/config/bundle
/// endpoints need; further capabilities (auth, runtime evaluation) attach
/// additional state as they are introduced.
pub struct ServerState {
/// The space's file storage (user files, with the bundle/base_fs layers
/// composed in by the caller).
pub space: Box<dyn SpacePrimitives>,
/// Read-only client bundle (HTML/CSS/JS) served at the SPA fallback. The
/// server cannot serve a usable UI without it, so it is required — callers
/// must fail at startup rather than construct an `ServerState` without one.
pub client_bundle: Box<dyn SpacePrimitives>,
/// Boot configuration returned from `/.config`.
pub boot_config: BootConfig,
/// Absolute path of the space folder, surfaced in `X-Space-Path` headers.
pub space_folder_path: String,
/// Server version, surfaced in `/.ping`'s `X-Server-Version`. The client
/// compares this against its compiled-in `publicVersion`; a mismatch shows a
/// "new version available" banner, so it must track the served bundle.
pub version: ServerVersion,
/// URL prefix the server is mounted under (e.g. `/wiki`), injected into the
/// `index.html` `<base href>`. Empty for a root-mounted server.
pub host_url_prefix: String,
/// Extra HTML injected into the `<head>` of the served `index.html`
/// (`SB_HEAD_HTML`). Empty by default.
pub additional_head_html: String,
/// PWA theme color (`SB_THEME_COLOR`), surfaced in the generated
/// `manifest.json`. Defaults to `#e1e1e1`.
pub theme_color: String,
/// Space description (`SB_DESCRIPTION`), surfaced in the generated
/// `manifest.json`.
pub space_description: String,
/// Authentication strategy for protected routes. `None` means the server is
/// open (no authentication).
pub authorizer: Option<Arc<dyn RequestAuthorizer>>,
/// The login flow's issuing side (standalone server). `None` mirrors
/// `authorizer == None` (an open server). Shares the `Authenticator` with
/// the `authorizer` via `Arc`.
pub login: Option<Arc<crate::auth::LoginManager>>,
/// Shell-execution policy for `/.shell`.
pub shell: ShellConfig,
/// Request metrics. `None` disables counting and `/metrics`.
pub metrics: Option<Arc<Metrics>>,
/// Lua runtime backend for `/.runtime/*`. May be disabled, when None: those endpoints 503.
pub runtime: Option<Box<dyn RuntimeBackend>>,
}