From 593f8ec279a456bde747028002df733332cc6870 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:36:59 +0200 Subject: [PATCH] Create self-contained renderer CLI --- .gitignore | 3 ++- Cargo.toml | 3 +++ src/bin/generator.rs | 25 +++++++++++++++++++++++++ src/bin/server.rs | 16 ++++++++++++++++ src/lib.rs | 32 +++++++++++++++++++++++++++++++- src/web.rs | 2 +- 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/bin/generator.rs create mode 100644 src/bin/server.rs diff --git a/.gitignore b/.gitignore index 7ac4791..a41c19c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -/uptime.db \ No newline at end of file +/*.db +/perf.data* diff --git a/Cargo.toml b/Cargo.toml index 0387aee..1516fd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +debug = 1 + [dependencies] askama = { version = "0.12.0", default-features = false } axum = { version = "0.6.20", default-features = false, features = [ diff --git a/src/bin/generator.rs b/src/bin/generator.rs new file mode 100644 index 0000000..b84b9dc --- /dev/null +++ b/src/bin/generator.rs @@ -0,0 +1,25 @@ +use eyre::WrapErr; +use std::io::{self, Write}; + +#[macro_use] +extern crate tracing; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + let (_, db) = uptime::init().await?; + + info!("Computing result"); + + let result = uptime::web::render_root(db) + .await + .wrap_err("rendering result")?; + + if let Err(io) = io::stdout().lock().write_all(result.as_bytes()) { + if io.kind() == io::ErrorKind::BrokenPipe { + return Ok(()); + } + return Err(io).wrap_err("writing output"); + } + + Ok(()) +} diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..a4ece08 --- /dev/null +++ b/src/bin/server.rs @@ -0,0 +1,16 @@ +#[tokio::main] +async fn main() -> eyre::Result<()> { + let (config, db) = uptime::init().await?; + + let checker = uptime::check_timer(config, db.clone()); + let server = uptime::axum_server(db); + + tokio::select! { + result = checker => { + result.map(|ok| match ok {}) + } + result = server => { + result + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5e27255..d4db120 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ extern crate tracing; mod client; mod config; pub mod db; -mod web; +pub mod web; use eyre::Context; use eyre::Result; @@ -20,6 +20,36 @@ pub use web::axum_server; const USER_AGENT: &str = concat!("github:Nilstrieb/uptime/", env!("GIT_COMMIT")); const VERSION: &str = env!("GIT_COMMIT"); +pub async fn init() -> Result<(Config, Arc>)> { + tracing_subscriber::fmt().init(); + + let version = env!("GIT_COMMIT"); + info!("Starting up uptime {version}"); + + let config_path = std::env::var("UPTIME_CONFIG_PATH").unwrap_or_else(|_| "uptime.json".into()); + + info!("Loading reading config"); + let mut config = crate::read_config(&config_path)?; + + let db_url = std::env::var("UPTIME_DB_URL"); + if let Ok(db_url) = db_url { + config.db_url = db_url; + } + + info!("Opening db"); + let db = crate::db::open_db(&config.db_url).await?; + let db = Arc::new(db); + + info!("Running migrations"); + + crate::db::MIGRATOR + .run(&*db) + .await + .wrap_err("running migrations")?; + + Ok((config, db)) +} + pub async fn check_timer(config: Config, db: Arc>) -> Result<ⵑ> { let req_client = reqwest::Client::builder() .use_rustls_tls() diff --git a/src/web.rs b/src/web.rs index 314e1e3..aee311b 100644 --- a/src/web.rs +++ b/src/web.rs @@ -46,7 +46,7 @@ async fn root(State(db): State>>) -> Response { }) } -async fn render_root(db: Arc>) -> Result { +pub async fn render_root(db: Arc>) -> Result { let checks = crate::db::get_checks(&db).await?; let status = compute_status(checks);