mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 11:21:24 +00:00
Allow configuring pict-rs through serializable types
This commit is contained in:
parent
e0cb7f695e
commit
3c844d86c5
4 changed files with 94 additions and 33 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -1591,7 +1591,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "pict-rs"
|
||||
version = "0.4.0-beta.8"
|
||||
version = "0.4.0-beta.9"
|
||||
dependencies = [
|
||||
"actix-form-data",
|
||||
"actix-rt",
|
||||
|
@ -2028,9 +2028,9 @@ checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.151"
|
||||
version = "1.0.152"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0"
|
||||
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
@ -2047,9 +2047,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.151"
|
||||
version = "1.0.152"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8"
|
||||
checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "pict-rs"
|
||||
description = "A simple image hosting service"
|
||||
version = "0.4.0-beta.8"
|
||||
version = "0.4.0-beta.9"
|
||||
authors = ["asonix <asonix@asonix.dog>"]
|
||||
license = "AGPL-3.0"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use clap::Parser;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod commandline;
|
||||
mod defaults;
|
||||
|
@ -16,16 +16,53 @@ pub(crate) use primitives::{
|
|||
AudioCodec, Filesystem, ImageFormat, LogFormat, ObjectStorage, Store, VideoCodec,
|
||||
};
|
||||
|
||||
pub(crate) fn configure_without_clap<P: AsRef<Path>, Q: AsRef<Path>>(
|
||||
config_file: Option<P>,
|
||||
/// Source for pict-rs configuration when embedding as a library
|
||||
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>,
|
||||
) -> color_eyre::Result<(Configuration, Operation)> {
|
||||
let config = Config::builder().add_source(config::Config::try_from(&Defaults::default())?);
|
||||
|
||||
let config = if let Some(config_file) = config_file {
|
||||
config.add_source(config::File::from(config_file.as_ref()))
|
||||
} else {
|
||||
config
|
||||
let config = match source {
|
||||
ConfigSource::Empty => config,
|
||||
ConfigSource::File { path } => config.add_source(config::File::from(path.as_ref())),
|
||||
ConfigSource::Memory { values } => config.add_source(config::Config::try_from(&values)?),
|
||||
};
|
||||
|
||||
let built = config
|
||||
|
|
56
src/lib.rs
56
src/lib.rs
|
@ -73,6 +73,8 @@ use self::{
|
|||
stream::{StreamLimit, StreamTimeout},
|
||||
};
|
||||
|
||||
pub use self::config::ConfigSource;
|
||||
|
||||
const MEGABYTES: usize = 1024 * 1024;
|
||||
const MINUTES: u32 = 60;
|
||||
const HOURS: u32 = 60 * MINUTES;
|
||||
|
@ -1162,28 +1164,50 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// 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`
|
||||
///
|
||||
/// When running pict-rs as a library, configuration is limited to environment variables and
|
||||
/// configuration files. Commandline options are not available.
|
||||
pub fn init_config<P: AsRef<Path>, Q: AsRef<Path>>(
|
||||
config_file: Option<P>,
|
||||
save_to: Option<Q>,
|
||||
) -> color_eyre::Result<()> {
|
||||
let (config, operation) = config::configure_without_clap(config_file, save_to)?;
|
||||
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
|
||||
/// 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`
|
||||
///
|
||||
/// When running pict-rs as a library, configuration is limited to environment variables and
|
||||
/// configuration files. Commandline options are not available.
|
||||
///
|
||||
/// ```rust
|
||||
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// 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
|
||||
.set((config, operation))
|
||||
.unwrap_or_else(|_| panic!("CONFIG cannot be initialized more than once"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Install the default pict-rs tracer
|
||||
|
|
Loading…
Reference in a new issue