forceful server shutdown

This commit is contained in:
2025-03-13 16:15:05 +01:00
parent 0242b29f3c
commit fa0d511bd6
4 changed files with 49 additions and 6 deletions

View File

@ -1,7 +1,11 @@
use signal_hook::{consts::*, iterator::Signals};
use std::{
collections::HashMap,
error::Error,
io::{BufRead, BufReader, Write},
net::{TcpListener, TcpStream},
process::exit,
thread,
};
#[derive(PartialEq, Eq, Debug)]
@ -342,18 +346,29 @@ fn handle_request(mut stream: TcpStream) {
// TODO: Act upon the request
stream
.write_all(b"HTTP/1.1 200 OK\r\n\r\nThis is the server speaking\r\n")
.write_all(b"HTTP/1.1 200 OK\r\n\r\nHello, World!\r\n")
.unwrap();
}
fn main() -> Result<(), std::io::Error> {
fn main() -> Result<(), Box<dyn Error>> {
let mut signals = Signals::new([SIGINT, SIGTERM])?;
// TODO: Gracefully shutdown server
thread::spawn(move || {
for sig in signals.forever() {
println!("Received signal {:?}", sig);
println!("Shutting down");
exit(1);
}
});
let listener = TcpListener::bind("0.0.0.0:8080")?;
println!("Server started");
for stream in listener.incoming() {
let stream = stream?;
handle_request(stream)
handle_request(stream);
}
Ok(())
}