alert2muc/src/main.rs

106 lines
2.7 KiB
Rust
Raw Normal View History

use std::net::SocketAddr;
use askama::Template;
2022-12-17 00:17:53 +01:00
use axum::{
extract::State,
routing::post,
http::StatusCode,
response::{IntoResponse, Response},
Json, Router,
};
use serde::Deserialize;
mod jabber;
2022-12-28 01:18:13 +01:00
mod prometheus_alert;
2022-12-28 01:27:49 +01:00
mod apprise_notify;
2022-12-17 00:17:53 +01:00
2022-12-28 01:18:13 +01:00
enum Payload {
Prometheus(prometheus_alert::Payload),
2022-12-28 01:27:49 +01:00
Apprise(apprise_notify::Notification),
}
2022-12-28 01:18:13 +01:00
impl Payload {
fn render(&self) -> Result<String, askama::Error> {
match self {
Payload::Prometheus(payload) => payload.render(),
2022-12-28 01:27:49 +01:00
Payload::Apprise(notification) => notification.render(),
2022-12-28 01:18:13 +01:00
}
}
2022-12-17 00:17:53 +01:00
2022-12-28 01:18:13 +01:00
async fn handle_payload(&self, jabber: jabber::Handle) -> Response {
let mut error_message = None;
2022-12-17 00:17:53 +01:00
2022-12-28 01:18:13 +01:00
match self.render() {
Ok(message) => {
jabber.send_message(message).await;
}
Err(e) => {
error_message = Some(format!("{}", e));
2022-12-17 00:17:53 +01:00
}
}
2022-12-28 01:18:13 +01:00
match error_message {
None =>
StatusCode::OK.into_response(),
Some(error_message) =>
(StatusCode::INTERNAL_SERVER_ERROR,
error_message
).into_response()
}
2022-12-17 00:17:53 +01:00
}
}
2022-12-28 01:27:49 +01:00
async fn prometheus_alerts(
2022-12-28 01:18:13 +01:00
State(jabber): State<jabber::Handle>,
Json(payload): Json<prometheus_alert::Payload>,
) -> Response {
Payload::Prometheus(payload)
.handle_payload(jabber)
.await
}
2022-12-28 01:27:49 +01:00
async fn apprise_notification(
State(jabber): State<jabber::Handle>,
Json(notification): Json<apprise_notify::Notification>,
) -> Response {
Payload::Apprise(notification)
.handle_payload(jabber)
.await
}
2022-12-17 00:46:47 +01:00
#[derive(Deserialize)]
struct Config {
listen_port: u16,
jid: String,
password: String,
muc: String,
}
2022-12-17 00:17:53 +01:00
#[tokio::main]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
2022-12-17 00:58:05 +01:00
let config: Config = serde_json::from_str(
2022-12-17 00:46:47 +01:00
&std::fs::read_to_string(
2022-12-17 00:52:36 +01:00
std::env::args().nth(1)
2022-12-17 00:46:47 +01:00
.expect("Call with config.json")
).expect("read config")
).expect("parse config");
2022-12-17 00:58:05 +01:00
let jabber = jabber::run(config.jid, config.password, config.muc).await;
2022-12-17 00:17:53 +01:00
// build our application with a route
let app = Router::new()
2022-12-28 01:27:49 +01:00
.route("/alert", post(prometheus_alerts))
.route("/notify", post(apprise_notification))
.with_state(jabber);
2022-12-17 00:17:53 +01:00
2022-12-17 00:58:05 +01:00
let addr = SocketAddr::from(([127, 0, 0, 1], config.listen_port));
2022-12-17 00:17:53 +01:00
tracing::debug!("listening on {}", addr);
2022-12-17 00:20:30 +01:00
let server = axum::Server::bind(&addr)
.serve(app.into_make_service());
systemd::daemon::notify(false, [(systemd::daemon::STATE_READY, "1")].iter())
.unwrap();
server.await
2022-12-17 00:17:53 +01:00
.unwrap();
}