Skip to content

Commit

Permalink
feat: update httplib.h, handle new env vars
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Cecchi <[email protected]>
  • Loading branch information
leonardoce committed Aug 2, 2023
1 parent 2d35e51 commit a3fac95
Show file tree
Hide file tree
Showing 3 changed files with 3,881 additions and 1,827 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Environment variables are your friends:
* `SQL_QUERY` is the SQL query that will be executed when accessing the `/tx`
endpoint.

* `KEEP_ALIVE_MAX_COUNT` TCP keepalive configuration

* `KEEP_ALIVE_TIMEOUT` TCP keepalive configuration

* `READ_TIMEOUT` HTTP request handling read timeout

* `WRITE_TIMEOUT` HTTP request handling write timeout

* `IDLE_INTERVAL` HTTP request timeout idle interval

The endpoint `/.well-known/check` will always return "ok".

## Other considerations
Expand Down
27 changes: 27 additions & 0 deletions http_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ void ok(const httplib::Request &req, httplib::Response &res);
void logger(const httplib::Request &req, const httplib::Response &res);
std::string get_configuration(const std::string &env_name,
const std::string &default_value = "");
void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter);

/**
* The database URL to be used as a libpq connection string
*/
const std::string database_url = get_configuration("DATABASE_URL");
const std::string sql_query = get_configuration("SQL_QUERY", "SELECT 1");
const std::string keep_alive_max_count = get_configuration("KEEP_ALIVE_MAX_COUNT");
const std::string keep_alive_timeout = get_configuration("KEEP_ALIVE_TIMEOUT");

/**
* Everything starts from here
Expand All @@ -45,11 +48,35 @@ int main() {
httplib::Server svr;
svr.Get("/tx", tx);
svr.Get("/.well-known/check", ok);

// Keepalive settings
set_server_option_from_env("KEEP_ALIVE_MAX_COUNT", [&svr](int value){ svr.set_keep_alive_max_count(value); });
set_server_option_from_env("KEEP_ALIVE_TIMEOUT", [&svr](int value){ svr.set_keep_alive_timeout(value); });

// Timeout settings
set_server_option_from_env("READ_TIMEOUT", [&svr](int value){ svr.set_read_timeout(value); });
set_server_option_from_env("WRITE_TIMEOUT", [&svr](int value){ svr.set_write_timeout(value); });
set_server_option_from_env("IDLE_INTERVAL", [&svr](int value){ svr.set_idle_interval(value); });

svr.listen("0.0.0.0", 8080);

return 0;
}

void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter) {
const std::string env_value = get_configuration(env_name);

if (!env_value.empty()) {
try {
std::cout << env_name << ": " << std::quoted(env_value) << std::endl;
setter(std::stoi(env_value));
} catch(std::exception &e) {
std::cout << "Error parsing " << env_name << " as an integer" << std::endl;
}
}

}

/**
* This is a context class that to be used inside a request handler. It will
* log the request handling time
Expand Down
Loading

0 comments on commit a3fac95

Please sign in to comment.