POST /die to Die

This commit is contained in:
lda committed 2025-05-22 04:27:04 +07:00
1 parent 0fd76042f0
commit 7806fba9e6
1 file changed
+34 -5
+34 -5
View File
@@ -1,7 +1,13 @@
use std::{collections::HashMap, error::Error as _};
use std::{
collections::HashMap,
error::Error as _,
sync::{Arc, Mutex},
};
use actix_cors::Cors;
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, get, http::header, web};
use actix_web::{
App, HttpRequest, HttpResponse, HttpServer, Responder, get, http::header, post, web,
};
use chrono::{NaiveDate, TimeDelta};
use fancy_regex::Regex;
use icalendar::{Calendar, CalendarComponent, Component};
@@ -164,7 +170,10 @@ fn filter_events_by_date_and_regex(
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let shutdown_handle = Arc::new(Mutex::new(None));
let shutdown_handle_clone = shutdown_handle.clone();
let server = HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
.allowed_methods(vec!["GET"])
@@ -173,12 +182,32 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(cors)
.app_data(web::Data::new(chrono::Local::now().date_naive())) // how to freeze date
.app_data(web::Data::new(shutdown_handle_clone.clone()))
.service(filtered_ics)
.service(die)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
.run();
*shutdown_handle.lock().unwrap() = Some(server.handle());
server.await
}
mod asspull;
mod funny_timer;
use actix_web::dev::ServerHandle;
#[post("/die")]
async fn die(shutdown: web::Data<Arc<Mutex<Option<ServerHandle>>>>) -> impl Responder {
// Trigger shutdown in a new task so we can respond first
if let Some(handle) = shutdown.lock().unwrap().take() {
actix_web::rt::spawn(async move {
handle.stop(true).await;
});
println!("dieing now.");
HttpResponse::Ok().body("Server is shutting down gracefully.")
} else {
HttpResponse::ServiceUnavailable().body("Shutdown already in progress or not available.")
}
}