From fd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 26 Sep 2024 17:31:16 -0500 Subject: feat!: impl full api --- src/main.rs | 93 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 33 deletions(-) (limited to 'src/main.rs') 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> { + 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, - pub address: Option, - pub services: Vec, + 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> { + 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()), + } + } } -- cgit v1.2.3-70-g09d2