@@ -25,6 +25,7 @@ void ok(const httplib::Request &req, httplib::Response &res);
2525void logger (const httplib::Request &req, const httplib::Response &res);
2626std::string get_configuration (const std::string &env_name,
2727 const std::string &default_value = " " );
28+ void set_server_option_from_env (const std::string& env_name, std::function<void (int )>setter);
2829
2930/* *
3031 * The database URL to be used as a libpq connection string
@@ -45,11 +46,39 @@ int main() {
4546 httplib::Server svr;
4647 svr.Get (" /tx" , tx);
4748 svr.Get (" /.well-known/check" , ok);
49+
50+ // Keepalive settings
51+ set_server_option_from_env (" KEEP_ALIVE_MAX_COUNT" , [&svr](int value){ svr.set_keep_alive_max_count (value); });
52+ set_server_option_from_env (" KEEP_ALIVE_TIMEOUT" , [&svr](int value){ svr.set_keep_alive_timeout (value); });
53+
54+ // Timeout settings
55+ set_server_option_from_env (" READ_TIMEOUT" , [&svr](int value){ svr.set_read_timeout (value); });
56+ set_server_option_from_env (" WRITE_TIMEOUT" , [&svr](int value){ svr.set_write_timeout (value); });
57+ set_server_option_from_env (" IDLE_INTERVAL" , [&svr](int value){ svr.set_idle_interval (value); });
58+
4859 svr.listen (" 0.0.0.0" , 8080 );
4960
5061 return 0 ;
5162}
5263
64+ /* *
65+ * This is an helper function to set an HTTP server property from
66+ * an environment variable.
67+ * The property to be set is an integer.
68+ */
69+ void set_server_option_from_env (const std::string& env_name, std::function<void (int )>setter) {
70+ const std::string env_value = get_configuration (env_name);
71+
72+ if (!env_value.empty ()) {
73+ try {
74+ std::cout << env_name << " : " << std::quoted (env_value) << std::endl;
75+ setter (std::stoi (env_value));
76+ } catch (std::exception &e) {
77+ std::cout << " Error parsing " << env_name << " as an integer" << std::endl;
78+ }
79+ }
80+ }
81+
5382/* *
5483 * This is a context class that to be used inside a request handler. It will
5584 * log the request handling time
0 commit comments