mirror of
https://github.com/DefectingCat/candy
synced 2025-07-15 16:51:34 +00:00
refactor(config): refacotr config reader
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@
|
||||
logs
|
||||
.DS_Store
|
||||
config.toml
|
||||
/html
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -26,12 +26,6 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.80"
|
||||
@ -80,15 +74,14 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
||||
name = "candy"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"config",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"toml",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
@ -12,13 +12,13 @@ http-body-util = "0.1.1"
|
||||
hyper-util = { version = "0.1.3", features = ["full"] }
|
||||
# tools
|
||||
thiserror = "1.0.59"
|
||||
anyhow = "1.0.82"
|
||||
# anyhow = "1.0.82"
|
||||
config = { version = "0.14.0", default-features = false, features = [
|
||||
"async",
|
||||
"toml",
|
||||
] }
|
||||
serde = "1.0.198"
|
||||
serde_derive = "1.0.198"
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
toml = "0.8.12"
|
||||
# logging
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||
|
@ -1 +1,9 @@
|
||||
port = "4000"
|
||||
[[host]]
|
||||
ip = "0.0.0.0"
|
||||
port = 4000
|
||||
index = "index.html"
|
||||
[host.route]
|
||||
# route path: GET /
|
||||
location = "/"
|
||||
# static file path
|
||||
root = "./html"
|
||||
|
@ -1,18 +1,28 @@
|
||||
use crate::error::Result;
|
||||
use config::Config;
|
||||
use serde_derive::Deserialize;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct SettingRoute {
|
||||
pub location: String,
|
||||
pub root: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct SettingHost {
|
||||
pub port: u32,
|
||||
pub route: SettingRoute,
|
||||
pub index: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct Settings {
|
||||
pub port: u32,
|
||||
pub host: Vec<SettingHost>,
|
||||
}
|
||||
|
||||
pub fn init_config() -> Result<Settings> {
|
||||
let config = Config::builder()
|
||||
.add_source(config::File::with_name("./config.toml"))
|
||||
.add_source(config::Environment::with_prefix("CANDY"))
|
||||
.build()?;
|
||||
|
||||
let settings: Settings = config.try_deserialize()?;
|
||||
let file = fs::read_to_string("./config.toml")?;
|
||||
let settings: Settings = toml::from_str(&file)?;
|
||||
Ok(settings)
|
||||
}
|
||||
|
@ -1,7 +1,11 @@
|
||||
use std::io;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("failed to parse config")]
|
||||
ConfigError(#[from] config::ConfigError),
|
||||
#[error("failed io")]
|
||||
IoError(#[from] io::Error),
|
||||
#[error("failed to decode toml")]
|
||||
TomlDecode(#[from] toml::de::Error),
|
||||
}
|
||||
|
||||
pub type Result<T, E = Error> = anyhow::Result<T, E>;
|
||||
|
@ -12,7 +12,6 @@ async fn main() -> Result<()> {
|
||||
init_logger();
|
||||
let settings = init_config().with_context(|| "init config failed")?;
|
||||
debug!("settings {:?}", settings);
|
||||
println!("Hello World");
|
||||
info!("Hello");
|
||||
Ok(())
|
||||
}
|
||||
|
@ -8,6 +8,5 @@ pub fn init_logger() {
|
||||
.with_writer(std::io::stdout);
|
||||
|
||||
let env_layer = EnvFilter::try_from_env("CANDY_LOG").unwrap_or_else(|_| "info".into());
|
||||
|
||||
registry().with(env_layer).with(formatting_layer).init();
|
||||
}
|
||||
|
Reference in New Issue
Block a user