summaryrefslogtreecommitdiff
path: root/dichroism/src/config.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2022-05-02 22:32:52 -0400
committerAdam T. Carpenter <atc@53hor.net>2022-05-02 22:32:52 -0400
commitc7d885e4b8189e4539c1f259a44ec37c6cd027b2 (patch)
treedc2b0486c123404854ae44fc49ac57f0eca18734 /dichroism/src/config.rs
parentf243a3b7341012227d6e8342a65f9c5d7784256f (diff)
downloadtheglassyladies-ssr.tar.xz
theglassyladies-ssr.zip
feat: reset button read config start db pool and launch axum serverssr
Diffstat (limited to 'dichroism/src/config.rs')
-rw-r--r--dichroism/src/config.rs62
1 files changed, 39 insertions, 23 deletions
diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs
index 2031ecc..225ee4c 100644
--- a/dichroism/src/config.rs
+++ b/dichroism/src/config.rs
@@ -1,32 +1,48 @@
-use crate::constants::DEFAULT_CONFIG;
use crate::result::Result;
-use once_cell::sync::Lazy;
-use serde::Deserialize;
-use std::env::var;
-use std::fs::File;
-use std::io::prelude::*;
-use std::net::SocketAddr;
-use toml::from_str;
+use anyhow::{anyhow, Context};
+use std::{env::var, net::SocketAddr, path::PathBuf};
-pub static CONFIG_INSTANCE: Lazy<Config> = Lazy::new(|| {
- Config::from_toml().unwrap_or_else(|e| {
- eprintln!("Error parsing config: {}", e.to_string());
- std::process::exit(1);
- })
-});
-
-#[derive(Debug, Clone, Deserialize)]
+#[derive(Debug, Clone)]
pub struct Config {
- pub db_url: String,
- pub img_root: String,
pub bind_addr: SocketAddr,
+ pub db_path: PathBuf,
+ pub img_path: PathBuf,
}
impl Config {
- pub fn from_toml() -> Result<Self> {
- let path = var("DICHROISM_CONFIG").unwrap_or_else(|_| String::from(DEFAULT_CONFIG));
- let mut config = String::new();
- File::open(path)?.read_to_string(&mut config)?;
- Ok(from_str(&config)?)
+ pub fn from_env() -> Result<Self> {
+ let config = Self {
+ bind_addr: var("GL_BIND_ADDR")
+ .with_context(|| "Bind address missing.")?
+ .parse()
+ .with_context(|| "Failed to parse bind address.")?,
+ db_path: var("GL_DB_PATH")
+ .with_context(|| "Database path missing.")?
+ .parse()
+ .with_context(|| "Failed to parse database path.")?,
+ img_path: var("GL_IMG_PATH")
+ .with_context(|| "Image store path missing.")?
+ .parse()
+ .with_context(|| "Failed to parse image store path.")?,
+ };
+
+ if config.db_path.is_dir() {
+ Err(anyhow!(format!(
+ "Database path {:?} is not a regular file",
+ config.db_path
+ )))
+ } else if !config.img_path.is_dir() {
+ Err(anyhow!(format!(
+ "Image store path {:?} is not a directory.",
+ config.img_path
+ )))
+ } else if config.img_path.read_dir().is_err() {
+ Err(anyhow!(format!(
+ "Image store path {:?} is not readable.",
+ config.img_path
+ )))
+ } else {
+ Ok(config)
+ }
}
}