summaryrefslogtreecommitdiff
path: root/server.cpp
blob: a454b68de8b6a99c110c5cfceeb86a01455c4733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Standard C++
#include <atomic>
#include <csignal>
#include <cstdlib>
#include <iostream>

// FastCGI
#include <fcgiapp.h>

using namespace std;

namespace {
static atomic_bool quit;
static atomic_bool reload;

static void
handler(int signal)
{
	if ( signal == SIGINT || signal == SIGTERM ) {
		quit = true;
	}
	else if ( signal == SIGHUP ) {
		reload = true;
	}
}

static void
init_signal_handler()
{
	struct sigaction sa;
	memset(&sa, 0, sizeof sa);
	sa.sa_handler = handler;
	sa.sa_flags   = 0;
	sigemptyset(&sa.sa_mask);

	sigaction(SIGINT, &sa, NULL);
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGHUP, &sa, NULL);
}

static void
load_configuration()
{
	cerr << "Load configuration ...\n";
}

static void
handle_request(const FCGX_Request& request)
{
	FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n");
	FCGX_FPrintF(request.out, "<h1>Verbindung erfolgreich verarbeitet!</h1>");
}
} // namespace

int
main()
{
	FCGX_Init();

	auto socket = FCGX_OpenSocket(":5000", 20);
	if ( socket == -1 ) {
		cerr << "Fehler beim Erstellen des Listen-Sockets...\n";
		return EXIT_FAILURE;
	}

	FCGX_Request request;
	if ( FCGX_InitRequest(&request, socket, FCGI_FAIL_ACCEPT_ON_INTR) != 0 ) {
		cerr << "Fehler beim Initialisieren...\n";
		return EXIT_FAILURE;
	}

	init_signal_handler();
	load_configuration();

	quit   = false;
	reload = false;

	cout << "Handle Requests...\n";

	while ( !quit ) {
		if ( reload ) {
			load_configuration();
			reload = false;
		}

		if ( FCGX_Accept_r(&request) < 0 ) {
			continue;
		}

		handle_request(request);

		FCGX_Finish_r(&request);
	}
	cout << "shutdown...\n";
}