diff options
author | metamuffin <metamuffin@disroot.org> | 2023-02-12 00:21:11 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-02-12 00:21:11 +0100 |
commit | 851ae4926fba296098fa5f08f99ba64622232ed2 (patch) | |
tree | b5d7809f8068fcb543b87d68aaf313d74612351a | |
parent | 2eae89fe70950b4c7f1e088f98d35d1bd2202058 (diff) | |
download | gnix-851ae4926fba296098fa5f08f99ba64622232ed2.tar gnix-851ae4926fba296098fa5f08f99ba64622232ed2.tar.bz2 gnix-851ae4926fba296098fa5f08f99ba64622232ed2.tar.zst |
config path as arg
-rw-r--r-- | src/config.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 7 |
2 files changed, 8 insertions, 6 deletions
diff --git a/src/config.rs b/src/config.rs index 6fc01d8..210a1e6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,6 @@ -use std::{collections::HashMap, fs::read_to_string, net::SocketAddr, path::PathBuf}; - use anyhow::Context; use serde::{Deserialize, Serialize}; +use std::{collections::HashMap, fs::read_to_string, net::SocketAddr, path::PathBuf}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { @@ -28,8 +27,8 @@ pub struct HostConfig { } impl Config { - pub fn load() -> anyhow::Result<Config> { - let raw = read_to_string("config.toml").context("reading config file")?; + pub fn load(path: &str) -> anyhow::Result<Config> { + let raw = read_to_string(path).context("reading config file")?; let config: Config = toml::from_str(&raw).context("parsing config")?; Ok(config) } diff --git a/src/main.rs b/src/main.rs index 976cad5..f507ed5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ pub mod config; use crate::config::Config; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use http_body_util::{combinators::BoxBody, BodyExt}; use hyper::{ body::Incoming, @@ -24,7 +24,10 @@ use tokio_rustls::TlsAcceptor; async fn main() -> anyhow::Result<()> { env_logger::init_from_env("LOG"); - let config = Arc::new(Config::load()?); + let config_path = std::env::args().skip(1).next().ok_or(anyhow!( + "first argument is expected to be the configuration file" + ))?; + let config = Arc::new(Config::load(&config_path)?); tokio::select! { x = serve_http(config.clone()) => x.context("serving http")?, |