mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 09:18:27 +00:00
Added SIGINT signal handler.
This commit is contained in:
parent
95c0a6d7a8
commit
6818f18275
1 changed files with 14 additions and 1 deletions
|
@ -7,14 +7,23 @@
|
|||
|
||||
#include <httpsvrkit.h>
|
||||
#include <cstdio>
|
||||
#include <signal.h>
|
||||
|
||||
using namespace httpsvrkit;
|
||||
|
||||
template<typename Fn> void signal(int sig, Fn fn)
|
||||
{
|
||||
static std::function<void ()> signal_handler_;
|
||||
struct SignalHandler { static void fn(int sig) { signal_handler_(); } };
|
||||
signal_handler_ = fn;
|
||||
signal(sig, SignalHandler::fn);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char* hi = "/hi";
|
||||
|
||||
HTTP_SERVER("localhost", 1234) {
|
||||
// svr, req, res
|
||||
|
||||
GET("/", {
|
||||
res.set_redirect(hi);
|
||||
|
@ -27,6 +36,10 @@ int main(void)
|
|||
GET("/dump", {
|
||||
res.set_content(dump_request(cxt));
|
||||
});
|
||||
|
||||
signal(SIGINT, [&](){
|
||||
svr->stop();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue