summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-09-26 17:31:16 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-09-26 17:31:16 -0500
commitfd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 (patch)
treef3e29427d1bbe4a8d6e050abbd9f66afb5fa2152 /src/main.rs
parentcbfca14b38806798847e3f2008038b25194a9b8b (diff)
feat!: impl full api
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs93
1 files changed, 60 insertions, 33 deletions
diff --git a/src/main.rs b/src/main.rs
index 2ff9fd3..97ed111 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,42 +1,69 @@
-use std::{fs::File, io::Write, path::PathBuf};
+use std::{fs::File, path::PathBuf};
+use tracing::level_filters::LevelFilter;
+use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
-use statsrv::Service;
+use statsrv::service::Services;
+
+#[cfg(not(debug_assertions))]
+const DEFAULT_CONFIG: &str = "/etc/statsrv.toml";
+#[cfg(debug_assertions)]
+const DEFAULT_CONFIG: &str = "./config.toml";
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ tracing_subscriber::registry()
+ .with(
+ EnvFilter::builder()
+ .with_default_directive(LevelFilter::INFO.into())
+ .from_env_lossy(),
+ )
+ .with(tracing_subscriber::fmt::layer())
+ .init();
+
+ let config = match Config::parse() {
+ Ok(c) => c,
+ Err(err) => {
+ tracing::debug!("Failed to read config file: `{err}`");
+ tracing::debug!("Using default config values");
+ Default::default()
+ }
+ };
+
+ let router = statsrv::router(config.root).with_state(config.services);
+
+ let listener = tokio::net::TcpListener::bind(config.address).await.unwrap();
+ tracing::info!("listening on {}", listener.local_addr().unwrap());
+
+ axum::serve(listener, router).await.map_err(Into::into)
+}
#[derive(Debug, Clone, serde::Deserialize)]
+#[serde(default)]
pub struct Config {
- pub title: String,
- pub template_path: PathBuf,
- pub output_dir: Option<PathBuf>,
- pub address: Option<String>,
- pub services: Vec<Service>,
+ pub root: PathBuf,
+ pub address: String,
+ pub services: Services,
}
-fn main() -> Result<(), main_error::MainError> {
- let mut args = std::env::args().skip(1);
-
- let config_path = args
- .next()
- .unwrap_or_else(|| "/etc/statsrv.toml".to_string());
- let config_file = File::open(config_path)?;
- let config_toml = std::io::read_to_string(config_file)?;
- let Config {
- title,
- template_path: template,
- output_dir,
- address: _,
- services,
- } = toml::from_str(&config_toml)?;
-
- let template_file = File::open(template)?;
- let template = std::io::read_to_string(template_file)?;
- let status_page = statsrv::generate(title, services, template);
-
- if let Some(output_dir) = output_dir {
- std::fs::create_dir_all(&output_dir)?;
- let mut html_writer = File::create(output_dir.join("index.html"))?;
-
- html_writer.write_all(status_page.as_bytes())?;
+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()
+ });
+
+ let config_file = File::open(&config_path)?;
+ let config_toml = std::io::read_to_string(config_file)?;
+ toml::from_str(&config_toml).map_err(Into::into)
}
+}
- Ok(())
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ root: PathBuf::from("./"),
+ address: String::from("127.0.0.1:8080"),
+ services: Services::new(Default::default()),
+ }
+ }
}