Simple notification http server
at main 1.2 kB view raw
1use actix_web::{App, HttpResponse, HttpServer, Responder, post, web}; 2use notify_rust::Notification; 3use serde::Deserialize; 4 5#[derive(Deserialize)] 6struct NotificationData { 7 name: String, 8 body: String, 9} 10 11#[post("/notify")] 12async fn notify(notification: web::Form<NotificationData>) -> impl Responder { 13 let notification = Notification::new() 14 .summary(&notification.name) 15 .body(&notification.body) 16 .timeout(0) 17 .show(); 18 19 match notification { 20 Ok(_) => HttpResponse::Ok().body("{\"status\": \"ok\"}"), 21 Err(err) => HttpResponse::InternalServerError() 22 .body(format!("{{\"status\": \"err\", \"err\": \"{}\"}}", err)), 23 } 24} 25 26#[actix_web::main] 27async fn main() -> std::io::Result<()> { 28 let host = std::env::var("HOST").unwrap_or(String::from("127.0.0.1")); 29 let port = std::env::var("PORT") 30 .map(|x| x.parse::<u16>().unwrap_or(60000)) 31 .unwrap_or(60000); 32 33 println!( 34 "Starting http server on POST http://{}:{}/notify", 35 host, port 36 ); 37 38 HttpServer::new(|| App::new().service(notify)) 39 .bind((host, port))? 40 .run() 41 .await 42}