summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-09-28 00:54:46 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-09-28 00:58:45 -0500
commitcd774827dd14f68d8405c45d2d9da30b3fab050e (patch)
treea24e1cabb99170caa25edff53fc978111a1c9dd4 /src/main.rs
parent04c7f7609e5bc3fadf95c53b37a9e6e12c4e539c (diff)
feat: refactor into pub-sub and impl SSE
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(),
}
}
}