2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Allow configuring pict-rs through serializable types

This commit is contained in:
asonix 2022-12-26 16:35:25 -06:00
parent e0cb7f695e
commit 3c844d86c5
4 changed files with 94 additions and 33 deletions

10
Cargo.lock generated
View file

@ -1591,7 +1591,7 @@ dependencies = [
[[package]] [[package]]
name = "pict-rs" name = "pict-rs"
version = "0.4.0-beta.8" version = "0.4.0-beta.9"
dependencies = [ dependencies = [
"actix-form-data", "actix-form-data",
"actix-rt", "actix-rt",
@ -2028,9 +2028,9 @@ checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.151" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@ -2047,9 +2047,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.151" version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -1,7 +1,7 @@
[package] [package]
name = "pict-rs" name = "pict-rs"
description = "A simple image hosting service" description = "A simple image hosting service"
version = "0.4.0-beta.8" version = "0.4.0-beta.9"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
license = "AGPL-3.0" license = "AGPL-3.0"
readme = "README.md" readme = "README.md"

View file

@ -1,5 +1,5 @@
use clap::Parser; use clap::Parser;
use std::path::Path; use std::path::{Path, PathBuf};
mod commandline; mod commandline;
mod defaults; mod defaults;
@ -16,16 +16,53 @@ pub(crate) use primitives::{
AudioCodec, Filesystem, ImageFormat, LogFormat, ObjectStorage, Store, VideoCodec, AudioCodec, Filesystem, ImageFormat, LogFormat, ObjectStorage, Store, VideoCodec,
}; };
pub(crate) fn configure_without_clap<P: AsRef<Path>, Q: AsRef<Path>>( /// Source for pict-rs configuration when embedding as a library
config_file: Option<P>, pub enum ConfigSource<P, T> {
/// A File source for pict-rs configuration
File { path: P },
/// An in-memory source for pict-rs configuration
Memory { values: T },
/// No configuration
Empty,
}
impl<T> ConfigSource<PathBuf, T>
where
T: serde::Serialize,
{
/// Create a new memory source
pub fn memory(values: T) -> Self {
Self::Memory { values }
}
}
impl<P> ConfigSource<P, ()>
where
P: AsRef<Path>,
{
/// Create a new file source
pub fn file(path: P) -> Self {
Self::File { path }
}
}
impl ConfigSource<PathBuf, ()> {
/// Create a new empty source
pub fn empty() -> Self {
Self::Empty
}
}
pub(crate) fn configure_without_clap<P: AsRef<Path>, T: serde::Serialize, Q: AsRef<Path>>(
source: ConfigSource<P, T>,
save_to: Option<Q>, save_to: Option<Q>,
) -> color_eyre::Result<(Configuration, Operation)> { ) -> color_eyre::Result<(Configuration, Operation)> {
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?); let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
let config = if let Some(config_file) = config_file { let config = match source {
config.add_source(config::File::from(config_file.as_ref())) ConfigSource::Empty => config,
} else { ConfigSource::File { path } => config.add_source(config::File::from(path.as_ref())),
config ConfigSource::Memory { values } => config.add_source(config::Config::try_from(&values)?),
}; };
let built = config let built = config

View file

@ -73,6 +73,8 @@ use self::{
stream::{StreamLimit, StreamTimeout}, stream::{StreamLimit, StreamTimeout},
}; };
pub use self::config::ConfigSource;
const MEGABYTES: usize = 1024 * 1024; const MEGABYTES: usize = 1024 * 1024;
const MINUTES: u32 = 60; const MINUTES: u32 = 60;
const HOURS: u32 = 60 * MINUTES; const HOURS: u32 = 60 * MINUTES;
@ -1162,28 +1164,50 @@ where
Ok(()) Ok(())
} }
/// Initialize the pict-rs configuration impl<P: AsRef<Path>, T: serde::Serialize> ConfigSource<P, T> {
/// /// Initialize the pict-rs configuration
/// This takes an optional config_file path which is a valid pict-rs configuration file, and an ///
/// optional save_to path, which the generated configuration will be saved into. Since many /// This takes an optional config_file path which is a valid pict-rs configuration file, and an
/// parameters have defaults, it can be useful to dump a valid configuration with default values to /// optional save_to path, which the generated configuration will be saved into. Since many
/// see what is available for tweaking. /// parameters have defaults, it can be useful to dump a valid configuration with default values to
/// /// see what is available for tweaking.
/// This function must be called before `run` or `install_tracing` ///
/// /// This function must be called before `run` or `install_tracing`
/// When running pict-rs as a library, configuration is limited to environment variables and ///
/// configuration files. Commandline options are not available. /// When running pict-rs as a library, configuration is limited to environment variables and
pub fn init_config<P: AsRef<Path>, Q: AsRef<Path>>( /// configuration files. Commandline options are not available.
config_file: Option<P>, ///
save_to: Option<Q>, /// ```rust
) -> color_eyre::Result<()> { /// fn main() -> Result<(), Box<dyn std::error::Error>> {
let (config, operation) = config::configure_without_clap(config_file, save_to)?; /// pict_rs::ConfigSource::memory(serde_json::json!({
/// "server": {
/// "address": "127.0.0.1:8080"
/// },
/// "old_db": {
/// "path": "./old"
/// },
/// "repo": {
/// "type": "sled",
/// "path": "./sled-repo"
/// },
/// "store": {
/// "type": "filesystem",
/// "path": "./files"
/// }
/// })).init::<&str>(None)?;
///
/// Ok(())
/// }
/// ```
pub fn init<Q: AsRef<Path>>(self, save_to: Option<Q>) -> color_eyre::Result<()> {
let (config, operation) = config::configure_without_clap(self, save_to)?;
DO_CONFIG DO_CONFIG
.set((config, operation)) .set((config, operation))
.unwrap_or_else(|_| panic!("CONFIG cannot be initialized more than once")); .unwrap_or_else(|_| panic!("CONFIG cannot be initialized more than once"));
Ok(()) Ok(())
}
} }
/// Install the default pict-rs tracer /// Install the default pict-rs tracer