mirror of
https://github.com/Noratrieb/uptime.git
synced 2026-01-14 16:45:06 +01:00
Create self-contained renderer CLI
This commit is contained in:
parent
1f89aae3de
commit
593f8ec279
6 changed files with 78 additions and 3 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
/uptime.db
|
/*.db
|
||||||
|
/perf.data*
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,9 @@ edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = 1
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
askama = { version = "0.12.0", default-features = false }
|
askama = { version = "0.12.0", default-features = false }
|
||||||
axum = { version = "0.6.20", default-features = false, features = [
|
axum = { version = "0.6.20", default-features = false, features = [
|
||||||
|
|
|
||||||
25
src/bin/generator.rs
Normal file
25
src/bin/generator.rs
Normal 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
16
src/bin/server.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/lib.rs
32
src/lib.rs
|
|
@ -6,7 +6,7 @@ extern crate tracing;
|
||||||
mod client;
|
mod client;
|
||||||
mod config;
|
mod config;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
mod web;
|
pub mod web;
|
||||||
|
|
||||||
use eyre::Context;
|
use eyre::Context;
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
|
|
@ -20,6 +20,36 @@ pub use web::axum_server;
|
||||||
const USER_AGENT: &str = concat!("github:Nilstrieb/uptime/", env!("GIT_COMMIT"));
|
const USER_AGENT: &str = concat!("github:Nilstrieb/uptime/", env!("GIT_COMMIT"));
|
||||||
const VERSION: &str = 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<ⵑ> {
|
pub async fn check_timer(config: Config, db: Arc<Pool<Sqlite>>) -> Result<ⵑ> {
|
||||||
let req_client = reqwest::Client::builder()
|
let req_client = reqwest::Client::builder()
|
||||||
.use_rustls_tls()
|
.use_rustls_tls()
|
||||||
|
|
|
||||||
|
|
@ -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 checks = crate::db::get_checks(&db).await?;
|
||||||
|
|
||||||
let status = compute_status(checks);
|
let status = compute_status(checks);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue