Skip to content

Commit a3fac95

Browse files
committed
feat: update httplib.h, handle new env vars
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
1 parent 2d35e51 commit a3fac95

File tree

3 files changed

+3881
-1827
lines changed

3 files changed

+3881
-1827
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Environment variables are your friends:
1515
* `SQL_QUERY` is the SQL query that will be executed when accessing the `/tx`
1616
endpoint.
1717

18+
* `KEEP_ALIVE_MAX_COUNT` TCP keepalive configuration
19+
20+
* `KEEP_ALIVE_TIMEOUT` TCP keepalive configuration
21+
22+
* `READ_TIMEOUT` HTTP request handling read timeout
23+
24+
* `WRITE_TIMEOUT` HTTP request handling write timeout
25+
26+
* `IDLE_INTERVAL` HTTP request timeout idle interval
27+
1828
The endpoint `/.well-known/check` will always return "ok".
1929

2030
## Other considerations

http_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ void ok(const httplib::Request &req, httplib::Response &res);
2525
void logger(const httplib::Request &req, const httplib::Response &res);
2626
std::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
*/
3233
const std::string database_url = get_configuration("DATABASE_URL");
3334
const 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

Comments
 (0)