-
Notifications
You must be signed in to change notification settings - Fork 0
feat(environment): #36: configure environment variable settings globally #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aasheptunov
merged 18 commits into
master
from
feature/#36-configure-environment-variable-settings-globally
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9769c1c
Merge branch 'master' into feature/#36-configure-environment-variable…
aasheptunov b7b64cf
feat(app-config): #36: adding an app-config layer to work with enviro…
aasheptunov 25bbb49
chore(devcontainer): #36: adding the --env-file argument to the devco…
aasheptunov 2ffab6d
docs(readme): #36: adding information to the README about the automat…
aasheptunov 378dec3
refactor(environment): #36: convert `app-config` into a singleton cla…
aasheptunov 6944e7f
Merge branch 'master' into feature/#36-configure-environment-variable…
aasheptunov eab4313
docs(readme): #36: add the necessary step to create the .env file in …
aasheptunov 8244c77
fix(workflows): #36: fixing a pipeline error related to the missing r…
aasheptunov e8de3d1
refactor(environment): #36: add error handling to app-config, add log…
aasheptunov 221c5f4
ci(workflows): #36: add a step to create the .env file so that the de…
aasheptunov 7242eaa
docs(readme): #36: reverted the automatic change, as it was not neces…
aasheptunov 84dcbb9
fix(environment): #36: fixed a logic error in the error logging, as t…
aasheptunov 69b984f
Merge branch 'feature/#36-configure-environment-variable-settings-glo…
aasheptunov 67814bb
refactor(environment): #36: removal of an unused variable, since the …
aasheptunov c531bd7
docs(logging): #36: editing the comment to make it clearer
aasheptunov 0125b78
feat(environment): #36: add setters for variables, move the set logic…
aasheptunov 828e43c
feat(environment): #36: add a log message that appears when an error …
aasheptunov ef7a052
Merge branch 'master' into feature/#36-configure-environment-variable…
aasheptunov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| #include "app-config.h" | ||
|
|
||
| AppConfig::AppConfig() | ||
| { | ||
| setApiHost(getEnv("API_HOST", "0.0.0.0")); | ||
| setApiPort(getEnvInt("API_PORT", 80)); | ||
| setApiNumThreads(getEnvInt("API_NUMBER_OF_THREADS", 1)); | ||
| setDatabaseHost(getEnv("POSTGRES_HOST", "0.0.0.0")); | ||
| setDatabasePort(getEnv("POSTGRES_PORT", "5432")); | ||
| setDatabaseName(getEnv("POSTGRES_DB", "to-dos-api-cpp-db")); | ||
| setDatabaseUser(getEnv("POSTGRES_USER", "postgres")); | ||
| setDatabasePassword(getEnv("POSTGRES_PASSWORD", "password")); | ||
| } | ||
|
|
||
| AppConfig& AppConfig::GetInstance() | ||
| { | ||
| static AppConfig instance; | ||
| return instance; | ||
| } | ||
|
|
||
| std::string AppConfig::getEnv(std::string name, std::string defaultValue) | ||
| { | ||
| char* value = std::getenv(name.c_str()); | ||
|
|
||
| if (value) | ||
| { | ||
| return std::string(value); | ||
| } | ||
| else | ||
| { | ||
| LOG_WARN << "Failed to get the value of the " << name << " environment variable; the default value will be used: " << defaultValue; | ||
| return defaultValue; | ||
| } | ||
| } | ||
|
|
||
| uint32_t AppConfig::getEnvInt(std::string name, uint32_t defaultValue) | ||
| { | ||
| char* value = std::getenv(name.c_str()); | ||
|
|
||
| if (!value) | ||
| return defaultValue; | ||
|
|
||
| try | ||
| { | ||
| auto result = std::stoi(value); | ||
| auto limit = std::numeric_limits<uint32_t>::max(); | ||
|
|
||
| if (result < 0 || result > limit) | ||
| throw std::overflow_error( | ||
| "error: an attempt to write a " + name + " value which is larger than what can be stored in a " + std::to_string(limit) | ||
| ); | ||
|
|
||
| return static_cast<uint32_t>(result); | ||
| } | ||
| catch (const std::exception& e) | ||
| { | ||
| LOG_ERROR << e.what(); | ||
| return defaultValue; | ||
| } | ||
| } | ||
|
|
||
| trantor::Logger::LogLevel AppConfig::parseLogLevel(const std::string& level) | ||
| { | ||
| // kTrace < kDebug < kInfo < kWarn < kError | ||
| if (level == "TRACE") | ||
| return trantor::Logger::kTrace; | ||
| else if (level == "DEBUG") | ||
| return trantor::Logger::kDebug; | ||
| else if (level == "INFO") | ||
| return trantor::Logger::kInfo; | ||
| else if (level == "WARN") | ||
| return trantor::Logger::kWarn; | ||
| else | ||
| return trantor::Logger::kError; | ||
| } | ||
|
|
||
| void AppConfig::setApiHost(std::string apiHost) | ||
| { | ||
| apiHost_ = apiHost; | ||
| LOG_DEBUG << "Update value: apiHost_=" << apiHost_; | ||
| }; | ||
|
|
||
| void AppConfig::setApiPort(uint32_t apiPort) | ||
| { | ||
| apiPort_ = apiPort; | ||
| LOG_DEBUG << "Update value: apiPort_=" << apiPort_; | ||
| }; | ||
|
|
||
| void AppConfig::setApiNumThreads(uint32_t apiNumThreads) | ||
| { | ||
| uint32_t numberOfThreads = apiNumThreads; | ||
|
|
||
| if (numberOfThreads > 0) | ||
| { | ||
| apiNumThreads_ = numberOfThreads; | ||
| } | ||
| else | ||
| { | ||
| apiNumThreads_ = 1; | ||
| LOG_ERROR << "The number of allocated threads cannot be less than 1"; | ||
| } | ||
|
|
||
| LOG_DEBUG << "Update value: apiNumThreads_=" << apiNumThreads_; | ||
| }; | ||
|
|
||
| void AppConfig::setDatabaseHost(std::string databaseHost) | ||
| { | ||
| databaseHost_ = databaseHost; | ||
| LOG_DEBUG << "Update value: databaseHost_=" << databaseHost_; | ||
| }; | ||
|
|
||
| void AppConfig::setDatabasePort(std::string databasePort) | ||
| { | ||
| databasePort_ = databasePort; | ||
| LOG_DEBUG << "Update value: databasePort_=" << databasePort_; | ||
| }; | ||
|
|
||
| void AppConfig::setDatabaseName(std::string databaseName) | ||
| { | ||
| databaseName_ = databaseName; | ||
| LOG_DEBUG << "Update value: databaseName_=" << databaseName_; | ||
| }; | ||
|
|
||
| void AppConfig::setDatabaseUser(std::string databaseUser) | ||
| { | ||
| databaseUser_ = databaseUser; | ||
| LOG_DEBUG << "Update value: databaseUser_=" << databaseUser_; | ||
| }; | ||
|
|
||
| void AppConfig::setDatabasePassword(std::string databasePassword) | ||
| { | ||
| databasePassword_ = databasePassword; | ||
| LOG_DEBUG << "Update value: databasePassword_=" << databasePassword_; | ||
| }; | ||
|
|
||
| const std::string& AppConfig::getApiHost() const | ||
| { return apiHost_; } | ||
|
|
||
| const uint32_t& AppConfig::getApiPort() const | ||
| { return apiPort_; } | ||
|
|
||
| const uint32_t& AppConfig::getApiNumThreads() const | ||
| { return apiNumThreads_; } | ||
|
|
||
| const trantor::Logger::LogLevel AppConfig::getApiLogLevel() | ||
| { return parseLogLevel(getEnv("API_LOG_LEVEL", "INFO")); } | ||
|
|
||
| const std::string& AppConfig::getDatabaseHost() const | ||
| { return databaseHost_; } | ||
|
|
||
| const std::string& AppConfig::getDatabasePort() const | ||
| { return databasePort_; } | ||
|
|
||
| const std::string& AppConfig::getDatabaseName() const | ||
| { return databaseName_; } | ||
|
|
||
| const std::string& AppConfig::getDatabaseUser() const | ||
| { return databaseUser_; } | ||
|
|
||
| const std::string& AppConfig::getDatabasePassword() const | ||
| { return databasePassword_; } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.