Add correctly shutting down server and stopping threads

This commit is contained in:
Pablu23
2024-11-13 16:49:49 +01:00
parent a3a5280f17
commit c18d5f993d
8 changed files with 61 additions and 35 deletions

View File

@@ -1,18 +1,33 @@
#include "http.hpp"
#include "router.hpp"
#include <csignal>
#include <cstdlib>
#include <iostream>
using namespace http;
void HelloWorld(Request req, Response *res) {
void hello_world(Request req, Response *res) {
res->set_payload("Hello World!");
res->set_content_type("text/plain");
}
static Router router(8181);
void quit(int s) {
router.stop();
std::cout << "Stopping Server" << std::endl;
}
int main() {
Router router(8181);
struct sigaction sig_int_handler;
sig_int_handler.sa_handler = quit;
sigemptyset(&sig_int_handler.sa_mask);
sig_int_handler.sa_flags = 0;
sigaction(SIGINT, &sig_int_handler, nullptr);
// Allow all Methods
router.handle("GET /helloWorld", HelloWorld);
router.handle("GET /helloWorld", hello_world);
router.handle("GET /healthz", [](Request req, Response *res) {
res->set_status_code(StatusCode::OK);
res->set_payload(std::vector<std::byte>());
@@ -38,6 +53,7 @@ int main() {
res->set_content_type("text/plain");
});
std::cout << "Starting Server" << std::endl;
router.start();
return 0;
}