Skip to main content

wasmio/infrastructure/config/
mod.rs

1use std::net::SocketAddr;
2use std::path::PathBuf;
3use std::str::FromStr;
4
5use anyhow::Context;
6use config::Config;
7use serde::{Deserialize, Serialize};
8
9mod storage;
10pub use storage::StorageConfig;
11
12/// Configuration file for the application.
13#[derive(Debug, Clone, Deserialize, Serialize)]
14pub struct Cfg {
15    pub bind_addr: SocketAddr,
16
17    pub storage: StorageConfig,
18}
19
20impl Cfg {
21    /// Read the associated configuration env
22    #[allow(dead_code)]
23    pub fn from_env() -> anyhow::Result<Cfg> {
24        let file_location = dotenv::var("CONFIG_FILE_LOCATION")
25            .with_context(|| "`CONFIG_FILE_LOCATION` must be set.")?;
26
27        let settings = Config::builder()
28            .add_source(config::File::with_name(&file_location))
29            .add_source(
30                config::Environment::with_prefix("WASMIO")
31                    .try_parsing(false)
32                    .separator("_"),
33            )
34            .build()?;
35
36        let config = settings.try_deserialize::<Cfg>()?;
37
38        Ok(config)
39    }
40
41    #[allow(dead_code)]
42    pub fn hack() -> anyhow::Result<Cfg> {
43        Ok(Cfg {
44            bind_addr: SocketAddr::from_str("0.0.0.0:80")?,
45            storage: StorageConfig {
46                path: PathBuf::new().join("public").join("data"),
47            },
48        })
49    }
50}