From b59305a2c9b4b48e59ccaeafedf2a9d7aaab7516 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 20 Sep 2026 11:46:40 +0200 Subject: Besseres Konfigurationsmanagement --- server.cpp | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'server.cpp') diff --git a/server.cpp b/server.cpp index 4ad0a3f..a18d5a8 100644 --- a/server.cpp +++ b/server.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -18,7 +19,18 @@ using namespace std; namespace { struct Configuration { - int value; + int value; + string listen_address; +}; + +struct RequestError : runtime_error { + RequestError(int status, const string& meldung) + : runtime_error(meldung) + , status(status) + { + } + + int status; }; atomic_bool quit{ false }; @@ -55,7 +67,8 @@ bool load_configuration(Configuration& configuration) { cerr << "Load configuration ...\n"; - configuration.value = 1; + configuration.value = 1; + configuration.listen_address = "127.0.0.1:5000"; return true; } @@ -70,11 +83,11 @@ handle_request(char** env, const Configuration&, string_view body, ostream& out) } void -send_internal_error(const FCGX_Request& request) +send_error(const FCGX_Request& request, int status, const char* meldung) { - FCGX_FPrintF(request.out, "Status: 500 Internal Server Error\r\n"); + FCGX_FPrintF(request.out, "Status: %d\r\n", status); FCGX_FPrintF(request.out, "Content-Type: text/plain; charset=utf-8\r\n\r\n"); - FCGX_FPrintF(request.out, "Die Anfrage konnte nicht verarbeitet werden.\n"); + FCGX_FPrintF(request.out, "%s\n", meldung); } string @@ -97,7 +110,13 @@ main() { FCGX_Init(); - auto socket = FCGX_OpenSocket(":5000", 20); + Configuration configuration{}; + if ( !load_configuration(configuration) ) { + cerr << "Die Konfiguration konnte nicht geladen werden.\n"; + return EXIT_FAILURE; + } + + auto socket = FCGX_OpenSocket(configuration.listen_address.c_str(), 20); if ( socket == -1 ) { cerr << "Fehler beim Erstellen des Listen-Sockets...\n"; return EXIT_FAILURE; @@ -111,12 +130,6 @@ main() init_signal_handler(); - Configuration configuration{}; - if ( !load_configuration(configuration) ) { - cerr << "Die Konfiguration konnte nicht geladen werden.\n"; - return EXIT_FAILURE; - } - cout << "Handle Requests...\n"; while ( !quit ) { @@ -124,6 +137,10 @@ main() Configuration new_configuration{}; if ( load_configuration(new_configuration) ) { + if ( new_configuration.listen_address != configuration.listen_address ) { + cerr << "Listen-Adresse geaendert, wirksam erst nach Neustart.\n"; + } + configuration = std::move(new_configuration); } else { @@ -148,9 +165,13 @@ main() const string_view text = response.view(); FCGX_PutStr(text.data(), static_cast(text.size()), request.out); } + catch ( const RequestError& e ) { + FCGX_FPrintF(request.err, "Anfrage abgebrochen (%d): %s\n", e.status, e.what()); + send_error(request, e.status, e.what()); + } catch ( const exception& e ) { FCGX_FPrintF(request.err, "Fehler beim Verarbeiten der Anfrage: %s\n", e.what()); - send_internal_error(request); + send_error(request, 500, "Die Anfrage konnte nicht verarbeitet werden."); } FCGX_Finish_r(&request); -- cgit v1.3