@@ -25,12 +25,15 @@ 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
3132 */
3233const std::string database_url = get_configuration(" DATABASE_URL" );
3334const std::string sql_query = get_configuration(" SQL_QUERY" , " SELECT 1" );
35+ const std::string keep_alive_max_count = get_configuration(" KEEP_ALIVE_MAX_COUNT" );
36+ const std::string keep_alive_timeout = get_configuration(" KEEP_ALIVE_TIMEOUT" );
3437
3538/* *
3639 * Everything starts from here
@@ -45,11 +48,35 @@ int main() {
4548 httplib::Server svr;
4649 svr.Get (" /tx" , tx);
4750 svr.Get (" /.well-known/check" , ok);
51+
52+ // Keepalive settings
53+ set_server_option_from_env (" KEEP_ALIVE_MAX_COUNT" , [&svr](int value){ svr.set_keep_alive_max_count (value); });
54+ set_server_option_from_env (" KEEP_ALIVE_TIMEOUT" , [&svr](int value){ svr.set_keep_alive_timeout (value); });
55+
56+ // Timeout settings
57+ set_server_option_from_env (" READ_TIMEOUT" , [&svr](int value){ svr.set_read_timeout (value); });
58+ set_server_option_from_env (" WRITE_TIMEOUT" , [&svr](int value){ svr.set_write_timeout (value); });
59+ set_server_option_from_env (" IDLE_INTERVAL" , [&svr](int value){ svr.set_idle_interval (value); });
60+
4861 svr.listen (" 0.0.0.0" , 8080 );
4962
5063 return 0 ;
5164}
5265
66+ void set_server_option_from_env (const std::string& env_name, std::function<void (int )>setter) {
67+ const std::string env_value = get_configuration (env_name);
68+
69+ if (!env_value.empty ()) {
70+ try {
71+ std::cout << env_name << " : " << std::quoted (env_value) << std::endl;
72+ setter (std::stoi (env_value));
73+ } catch (std::exception &e) {
74+ std::cout << " Error parsing " << env_name << " as an integer" << std::endl;
75+ }
76+ }
77+
78+ }
79+
5380/* *
5481 * This is a context class that to be used inside a request handler. It will
5582 * log the request handling time
0 commit comments