mirror of
https://github.com/OMGeeky/twba.notifier.git
synced 2025-12-26 17:02:38 +01:00
init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target
|
||||
.idea
|
||||
3896
Cargo.lock
generated
Normal file
3896
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "twba-notifier"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
twba-common = { version = "0.2", git = "https://github.com/OMGeeky/twba.common.git" }
|
||||
|
||||
tracing = "0.1"
|
||||
tokio = { version = "1.33", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
reqwest = { version = "0.12.4" }
|
||||
actix-web = "4.5"
|
||||
serde = { version = "1.0.199", features = ["derive"] }
|
||||
serde_json = "1.0.116"
|
||||
92
src/main.rs
Normal file
92
src/main.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use actix_web::{get, post, web, App, HttpServer, Responder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use twba_common::prelude::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
config: Conf,
|
||||
}
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
struct NotificationWebhookMessage {
|
||||
content: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
struct NotificationRequest {
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
#[instrument]
|
||||
async fn index() -> impl Responder {
|
||||
"Hello, world!"
|
||||
}
|
||||
|
||||
#[post("/notify")]
|
||||
#[instrument(skip(data))]
|
||||
async fn post_notify(req_body: String, data: web::Data<AppState>) -> impl Responder {
|
||||
match post_notify_inner(req_body, data).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
async fn post_notify_inner(req_body: String, data: web::Data<AppState>) -> Result<String, String> {
|
||||
let req_body = serde_json::from_str::<NotificationRequest>(&req_body)
|
||||
.map_err(|e| format!("Could not parse request body: {e}"))
|
||||
.map(|req| NotificationWebhookMessage {
|
||||
content: req.message,
|
||||
})
|
||||
// .and_then(|msg| serde_json::to_string(&msg).map_err(|e| format!("Could not serialize message: {e}"))
|
||||
.map_err(|e| format!("Could not serialize message: {e}"))?;
|
||||
info!("req_body: {:?}", req_body);
|
||||
println!("req_body: {:?}", req_body);
|
||||
let webhook_url = &data.config.notifier.webhook_url;
|
||||
if let Some(webhook_url) = webhook_url {
|
||||
let req_body = NotificationWebhookMessage {
|
||||
content: req_body.content,
|
||||
};
|
||||
let response = notify_webhook(webhook_url, &req_body).await?;
|
||||
info!("response: {}", response);
|
||||
println!("response: {}", response);
|
||||
Ok(format!(
|
||||
"Sent something successfully. Return of webhook: '{}'",
|
||||
response
|
||||
))
|
||||
} else {
|
||||
Err("No webhook URL configured and smtp not supported yet".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
async fn notify_webhook(
|
||||
webhook_url: &str,
|
||||
req_body: &NotificationWebhookMessage,
|
||||
) -> Result<String, String> {
|
||||
reqwest::Client::new()
|
||||
.post(webhook_url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(serde_json::to_string(req_body).map_err(|e| {
|
||||
format!("Could not notify webhook (error parsing message to json): {e}")
|
||||
})?)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Could not notify webhook: {e}"))?
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| format!("Could not notify webhook (text response): {e}"))
|
||||
}
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let _guard = init_tracing("twba_common");
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.app_data(web::Data::new(AppState {
|
||||
config: get_config(),
|
||||
}))
|
||||
.service(index)
|
||||
.service(post_notify)
|
||||
})
|
||||
.bind(("0.0.0.0", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
Reference in New Issue
Block a user