summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-10-09 18:23:58 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-10-09 18:23:58 -0500
commitb94f8e694bf01f5dba9ce2c01f589463a3dfbc69 (patch)
treec787530e63fb510db31533166edf1b9ff54be62a /src/main.rs
parent117d33fc478bf529094850b1fe40c558f04c9865 (diff)
feat!: rewrite to use traits and streams
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs39
1 files changed, 16 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index fbf27cb..46adbfa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,14 +1,10 @@
-use std::{collections::HashMap, fs::File, path::PathBuf, sync::Arc};
+use std::{collections::HashMap, fs::File, path::PathBuf};
+
use tower_http::services::ServeDir;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
-use statsrv::service::Service;
-
-#[cfg(not(debug_assertions))]
-const DEFAULT_CONFIG: &str = "/etc/statsrv.toml";
-#[cfg(debug_assertions)]
-const DEFAULT_CONFIG: &str = "./config.toml";
+use statsrv::{service::ServiceConfig, AppState};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -20,20 +16,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = match Config::parse() {
Ok(c) => c,
Err(err) => {
- tracing::debug!("Failed to read config file: `{err}`");
- tracing::debug!("Using default config values");
+ tracing::error!("Failed to read config file, using defaults: `{err}`");
Default::default()
}
};
- let state = config
- .services
- .into_iter()
- .map(|(name, service)| (name, service.into()))
- .collect();
-
+ let state = AppState::spawn_services(config.services);
let router = statsrv::router()
- .with_state(Arc::new(state))
+ .with_state(state)
.nest_service("/", ServeDir::new(config.root))
.layer(tower_http::trace::TraceLayer::new_for_http());
@@ -48,17 +38,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
pub struct Config {
pub root: PathBuf,
pub address: String,
- pub services: HashMap<String, Service>,
+ pub services: HashMap<String, ServiceConfig>,
}
impl Config {
- fn parse() -> Result<Self, Box<dyn std::error::Error>> {
- let config_path = std::env::args().nth(1).unwrap_or_else(|| {
- tracing::debug!("Falling back to default config location");
- DEFAULT_CONFIG.to_string()
- });
+ const DEFAULT_CONFIG: &str = "/etc/statsrv.toml";
- let config_file = File::open(&config_path)?;
+ fn parse() -> Result<Self, Box<dyn std::error::Error>> {
+ let config_file = match std::env::args().nth(1) {
+ Some(p) => File::open(&p)?,
+ None => {
+ tracing::debug!("Falling back to default config location");
+ File::open(Self::DEFAULT_CONFIG)?
+ }
+ };
let config_toml = std::io::read_to_string(config_file)?;
toml::from_str(&config_toml).map_err(Into::into)
}