Create self-contained renderer CLI

This commit is contained in:
nora 2023-09-22 18:36:59 +02:00
parent 1f89aae3de
commit 593f8ec279
6 changed files with 78 additions and 3 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
/uptime.db
/*.db
/perf.data*

View file

@ -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 = [

25
src/bin/generator.rs Normal file
View file

@ -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(())
}

16
src/bin/server.rs Normal file
View file

@ -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
}
}
}

View file

@ -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<Pool<Sqlite>>)> {
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<Pool<Sqlite>>) -> Result<> {
let req_client = reqwest::Client::builder()
.use_rustls_tls()

View file

@ -46,7 +46,7 @@ async fn root(State(db): State<Arc<Pool<Sqlite>>>) -> Response {
})
}
async fn render_root(db: Arc<Pool<Sqlite>>) -> Result<String> {
pub async fn render_root(db: Arc<Pool<Sqlite>>) -> Result<String> {
let checks = crate::db::get_checks(&db).await?;
let status = compute_status(checks);