Skip to main content

surver/
lib.rs

1//! External access to the Surver server.
2use std::{sync::LazyLock, time::SystemTime};
3
4use serde::{Deserialize, Serialize};
5
6#[cfg(not(target_arch = "wasm32"))]
7mod server;
8#[cfg(not(target_arch = "wasm32"))]
9pub use server::surver_main;
10
11pub const HTTP_SERVER_KEY: &str = "Server";
12pub const HTTP_SERVER_VALUE_SURFER: &str = "Surfer";
13pub const X_WELLEN_VERSION: &str = "x-wellen-version";
14pub const X_SURFER_VERSION: &str = "x-surfer-version";
15pub const SURFER_VERSION: &str = env!("CARGO_PKG_VERSION");
16pub const WELLEN_VERSION: &str = wellen::VERSION;
17
18pub const WELLEN_SURFER_DEFAULT_OPTIONS: wellen::LoadOptions = wellen::LoadOptions {
19    multi_thread: true,
20    remove_scopes_with_empty_name: true,
21};
22
23#[derive(Debug, Deserialize)]
24pub struct SurverConfig {
25    /// IP address to bind the HTTP server to
26    pub bind_address: String,
27    /// Default port for the HTTP server
28    pub port: u16,
29}
30
31#[derive(Debug, Serialize, Deserialize)]
32pub struct SurverStatus {
33    pub wellen_version: String,
34    pub surfer_version: String,
35    pub file_infos: Vec<SurverFileInfo>,
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone)]
39pub struct SurverFileInfo {
40    pub bytes: u64,
41    pub bytes_loaded: u64,
42    pub filename: String,
43    pub format: wellen::FileFormat,
44    pub reloading: bool,
45    pub last_load_ok: bool,
46    pub last_modification_time: Option<SystemTime>,
47}
48
49impl SurverFileInfo {
50    #[must_use]
51    pub fn modification_time_string(&self) -> String {
52        modification_time_string(self.last_modification_time)
53    }
54}
55
56pub static BINCODE_OPTIONS: LazyLock<bincode::DefaultOptions> =
57    LazyLock::new(bincode::DefaultOptions::new);
58
59pub(crate) fn modification_time_string(mtime: Option<SystemTime>) -> String {
60    if let Some(mtime) = mtime {
61        let dur = mtime
62            .duration_since(std::time::UNIX_EPOCH)
63            .unwrap_or_default();
64        return chrono::DateTime::<chrono::Utc>::from_timestamp(
65            dur.as_secs().cast_signed(),
66            dur.subsec_nanos(),
67        )
68        .map_or_else(
69            || "Incorrect timestamp".to_string(),
70            |dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
71        );
72    }
73    "unknown".to_string()
74}