From 887adf5f2c51bb89fa450ce6d584e6eaf9e249df Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 28 Dec 2022 01:27:49 +0100 Subject: [PATCH] add apprise_notify format --- src/apprise_notify.rs | 10 ++++++++++ src/main.rs | 17 +++++++++++++++-- templates/apprise_notify.txt | 2 ++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/apprise_notify.rs create mode 100644 templates/apprise_notify.txt diff --git a/src/apprise_notify.rs b/src/apprise_notify.rs new file mode 100644 index 0000000..f19df7f --- /dev/null +++ b/src/apprise_notify.rs @@ -0,0 +1,10 @@ +use askama::Template; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize, Template)] +#[template(path="apprise_notify.txt", escape="none")] +pub struct Notification { + pub title: String, + pub message: String, + pub r#type: String, +} diff --git a/src/main.rs b/src/main.rs index 99570fc..084eb0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,15 +11,18 @@ use serde::Deserialize; mod jabber; mod prometheus_alert; +mod apprise_notify; enum Payload { Prometheus(prometheus_alert::Payload), + Apprise(apprise_notify::Notification), } impl Payload { fn render(&self) -> Result { match self { Payload::Prometheus(payload) => payload.render(), + Payload::Apprise(notification) => notification.render(), } } @@ -46,7 +49,7 @@ impl Payload { } } -async fn alerts( +async fn prometheus_alerts( State(jabber): State, Json(payload): Json, ) -> Response { @@ -55,6 +58,15 @@ async fn alerts( .await } +async fn apprise_notification( + State(jabber): State, + Json(notification): Json, +) -> Response { + Payload::Apprise(notification) + .handle_payload(jabber) + .await +} + #[derive(Deserialize)] struct Config { listen_port: u16, @@ -78,7 +90,8 @@ async fn main() { // build our application with a route let app = Router::new() - .route("/alert", post(alerts)) + .route("/alert", post(prometheus_alerts)) + .route("/notify", post(apprise_notification)) .with_state(jabber); let addr = SocketAddr::from(([127, 0, 0, 1], config.listen_port)); diff --git a/templates/apprise_notify.txt b/templates/apprise_notify.txt new file mode 100644 index 0000000..c5bb15e --- /dev/null +++ b/templates/apprise_notify.txt @@ -0,0 +1,2 @@ +[{{ type|upper }}] {{ title }} +> {{ message }}