bundle UI files into the binary

This is optional but now enabled for release builds.

Why?

* It shrinks the release docker images a bit, as the binary
  includes only the gzipped version of files and uncompressed into RAM
  at startup (which should be fast).

* It's a step toward #160.
This commit is contained in:
Scott Lamb
2023-08-04 12:52:05 -05:00
parent 02ac1a5570
commit faba358925
13 changed files with 534 additions and 61 deletions

View File

@@ -14,9 +14,6 @@ use crate::json::Permissions;
fn default_db_dir() -> PathBuf {
crate::DEFAULT_DB_DIR.into()
}
fn default_ui_dir() -> PathBuf {
"/usr/local/lib/moonfire-nvr/ui".into()
}
/// Top-level configuration file object.
#[derive(Debug, Deserialize)]
@@ -32,8 +29,9 @@ pub struct ConfigFile {
pub db_dir: PathBuf,
/// Directory holding user interface files (`.html`, `.js`, etc).
#[serde(default = "default_ui_dir")]
pub ui_dir: PathBuf,
#[cfg_attr(not(feature = "bundled-ui"), serde(default))]
#[cfg_attr(feature = "bundled-ui", serde(default))]
pub ui_dir: UiDir,
/// The number of worker threads used by the asynchronous runtime.
///
@@ -42,6 +40,34 @@ pub struct ConfigFile {
pub worker_threads: Option<usize>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum UiDir {
FromFilesystem(PathBuf),
Bundled(BundledUi),
}
impl Default for UiDir {
#[cfg(feature = "bundled-ui")]
fn default() -> Self {
UiDir::Bundled(BundledUi { bundled: true })
}
#[cfg(not(feature = "bundled-ui"))]
fn default() -> Self {
UiDir::FromFilesystem("/usr/local/lib/moonfire-nvr/ui".into())
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct BundledUi {
/// Just a marker to select this variant.
#[allow(unused)]
bundled: bool,
}
/// Per-bind configuration.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]

View File

@@ -24,7 +24,7 @@ use tracing::{info, warn};
use self::config::ConfigFile;
mod config;
pub mod config;
/// Runs the server, saving recordings and allowing web access.
#[derive(Bpaf, Debug)]