From 6f943a5398227e0e38133878275a63790d2732ba Mon Sep 17 00:00:00 2001 From: theMackabu Date: Sat, 25 Nov 2023 00:50:36 -0800 Subject: [PATCH] cleanup routes --- src/daemon/api.rs | 37 ++++++++++++++++++++++++++++++------- src/daemon/mod.rs | 2 +- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/daemon/api.rs b/src/daemon/api.rs index 076cd70..8e96f67 100644 --- a/src/daemon/api.rs +++ b/src/daemon/api.rs @@ -2,7 +2,13 @@ use macros_rs::fmtstr; use pmc::{config, process::Runner}; use serde::Serialize; use std::convert::Infallible; -use warp::{http::StatusCode, reject, reply::json, Filter, Rejection, Reply}; + +use warp::{ + http::StatusCode, + path, reject, + reply::{self, json}, + Filter, Rejection, Reply, +}; #[derive(Serialize)] struct ErrorMessage { @@ -13,11 +19,11 @@ struct ErrorMessage { pub async fn start() { let config = config::read().daemon.api; - let health = warp::path!("health").map(|| format!("ok!")); - let list = warp::path!("list").and_then(list_handler); - let info = warp::path!("info" / usize).and_then(|id| info_handler(id)); - let routes = warp::get().and(health.or(list).or(info)).recover(handle_rejection); + let health = path!("metrics").and_then(metrics_handler); + let list = path!("list").and_then(list_handler); + let info = path!("info" / usize).and_then(|id| info_handler(id)); + let routes = warp::get().and(health.or(list).or(info)).recover(handle_rejection); if config.secure.enabled { let auth = warp::header::exact("authorization", fmtstr!("token {}", config.secure.token)); warp::serve(routes.and(auth)).run(config::read().get_address()).await @@ -29,6 +35,23 @@ pub async fn start() { #[inline] async fn list_handler() -> Result { Ok(json(&Runner::new().json())) } +#[inline] +async fn metrics_handler() -> Result { + let response = + serde_json::json!({ + "healthy": true, + "version": { + "pkg": format!("v{}", env!("CARGO_PKG_VERSION")), + "hash": env!("GIT_HASH"), + "build_date": env!("BUILD_DATE"), + "target": env!("PROFILE"), + }, + "daemon": {} + }); + + Ok(json(&response)) +} + #[inline] async fn info_handler(id: usize) -> Result { match Runner::new().info(id) { @@ -53,10 +76,10 @@ async fn handle_rejection(err: Rejection) -> Result { message = "UNHANDLED_REJECTION"; } - let json = warp::reply::json(&ErrorMessage { + let json = json(&ErrorMessage { code: code.as_u16(), message: message.into(), }); - Ok(warp::reply::with_status(json, code)) + Ok(reply::with_status(json, code)) } diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index ff88d70..2bde249 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -253,7 +253,7 @@ pub fn start() { let callback = pmc::Callback(init); pmc::service::try_fork(false, false, callback); } else { - match daemon(false, true) { + match daemon(false, false) { Ok(Fork::Parent(_)) => {} Ok(Fork::Child) => init(), Err(err) => crashln!("{} Daemon creation failed with code {err}", *helpers::FAIL), -- GitLab