110 lines
2.3 KiB
Rust
110 lines
2.3 KiB
Rust
use serde::Deserialize;
|
|
use std::{num::NonZeroUsize, path::PathBuf};
|
|
use tracing::debug;
|
|
|
|
use crate::config::{
|
|
env::load_env,
|
|
logging::{LoggingFormat, LoggingInitializer, LoggingPreset, LoggingTarget, LokiConfig},
|
|
};
|
|
|
|
/// Note that the field of this struct are not capitalized.
|
|
/// Envy is case-insensitive, and expects Rust fields to be snake_case.
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct PileServerConfig {
|
|
#[serde(flatten)]
|
|
pub loki: Option<LokiConfig>,
|
|
|
|
/// The logging level to run with
|
|
#[serde(default)]
|
|
pub loglevel: LoggingPreset,
|
|
|
|
#[serde(default)]
|
|
pub logformat: LoggingFormat,
|
|
|
|
/// How many worker threads to use
|
|
pub threads: Option<NonZeroUsize>,
|
|
|
|
/// IP and port to bind to
|
|
/// Should look like `127.0.0.1:3030`
|
|
pub server_addr: String,
|
|
|
|
pub api_token: Option<String>,
|
|
pub workdir_root: PathBuf,
|
|
}
|
|
|
|
impl Default for PileServerConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
loki: None,
|
|
loglevel: LoggingPreset::Debug,
|
|
logformat: LoggingFormat::Ansi,
|
|
threads: None,
|
|
server_addr: "0.0.0.0:3000".into(),
|
|
api_token: None,
|
|
workdir_root: "./.pile".into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PileServerConfig {
|
|
pub fn load(with_env: bool, cli_log_level: LoggingPreset) -> Self {
|
|
let config = match with_env {
|
|
false => Self::default(),
|
|
true => {
|
|
let env = match load_env::<Self>() {
|
|
Ok(x) => x,
|
|
|
|
#[expect(clippy::print_stdout)]
|
|
Err(err) => {
|
|
println!("Error while loading .env: {err}");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
env.get_config().clone()
|
|
}
|
|
};
|
|
|
|
{
|
|
let res = LoggingInitializer {
|
|
app_name: "pile-server",
|
|
loki: config.loki.clone(),
|
|
preset: if with_env {
|
|
config.loglevel
|
|
} else {
|
|
cli_log_level
|
|
},
|
|
target: LoggingTarget::Stderr {
|
|
format: config.logformat,
|
|
},
|
|
}
|
|
.initialize();
|
|
|
|
if let Err(e) = res {
|
|
#[expect(clippy::print_stderr)]
|
|
for e in e.chain() {
|
|
eprintln!("{e}");
|
|
}
|
|
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
debug!(message = "Config loaded", ?config);
|
|
|
|
return config;
|
|
}
|
|
|
|
pub fn make_runtime(&self) -> tokio::runtime::Runtime {
|
|
let mut rt = tokio::runtime::Builder::new_multi_thread();
|
|
rt.enable_all();
|
|
if let Some(threads) = self.threads {
|
|
rt.worker_threads(threads.into());
|
|
}
|
|
|
|
#[expect(clippy::unwrap_used)]
|
|
let rt = rt.build().unwrap();
|
|
|
|
return rt;
|
|
}
|
|
}
|