summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs
index 97ed111..99af338 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,9 @@
-use std::{fs::File, path::PathBuf};
+use std::{collections::HashMap, fs::File, path::PathBuf, sync::Arc};
+use tower_http::services::ServeDir;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
-use statsrv::service::Services;
+use statsrv::service::Service;
#[cfg(not(debug_assertions))]
const DEFAULT_CONFIG: &str = "/etc/statsrv.toml";
@@ -29,7 +30,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
- let router = statsrv::router(config.root).with_state(config.services);
+ let state = config
+ .services
+ .into_iter()
+ .map(|(name, service)| (name, service.into()))
+ .collect();
+
+ let router = statsrv::router()
+ .with_state(Arc::new(state))
+ .nest_service("/", ServeDir::new(config.root))
+ .layer(tower_http::trace::TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind(config.address).await.unwrap();
tracing::info!("listening on {}", listener.local_addr().unwrap());
@@ -42,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
pub struct Config {
pub root: PathBuf,
pub address: String,
- pub services: Services,
+ pub services: HashMap<String, Service>,
}
impl Config {
@@ -63,7 +73,7 @@ impl Default for Config {
Self {
root: PathBuf::from("./"),
address: String::from("127.0.0.1:8080"),
- services: Services::new(Default::default()),
+ services: Default::default(),
}
}
}