From 606ea9e83998e334dc7619cded1c58f8135559f4 Mon Sep 17 00:00:00 2001 From: harshfeudal <87577447+harshfeudal@users.noreply.github.com> Date: Wed, 20 Sep 2023 07:57:30 +0700 Subject: [PATCH] [Fix]: `dotenv` issue --- .env.example | 9 +++- .vscode/settings.json | 89 ++++++++++++++++++++++++++++++++++++++ src/cores/database.cpp | 9 ++++ src/cores/dotenv.cpp | 97 ++++++++++-------------------------------- src/cores/dotenv.h | 7 +-- src/cores/harshie.cpp | 27 +++++++++--- src/cores/harshie.h | 3 ++ 7 files changed, 154 insertions(+), 87 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.env.example b/.env.example index 6c5a3e3..489d4ef 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,8 @@ -ENCODED_CLIENT_TOKEN=ADD_HERE \ No newline at end of file +ENCODED_CLIENT_TOKEN=ADD_HERE + +DBNAME=ADD_HERE +DBHOST=ADD_HERE +DBPORT=ADD_HERE +DBUSER=ADD_HERE +DBPASS=ADD_HERE +DPAPPNAME=ADD_HERE diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5d5a2e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,89 @@ +{ + "files.associations": { + "xstring": "cpp", + "algorithm": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "coroutine": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "resumable": "cpp", + "filesystem": "cpp", + "format": "cpp", + "forward_list": "cpp", + "fstream": "cpp", + "functional": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "queue": "cpp", + "ranges": "cpp", + "ratio": "cpp", + "shared_mutex": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "valarray": "cpp", + "variant": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" + } +} \ No newline at end of file diff --git a/src/cores/database.cpp b/src/cores/database.cpp index 18b0d9e..bb76e6d 100644 --- a/src/cores/database.cpp +++ b/src/cores/database.cpp @@ -18,15 +18,24 @@ #include #include +#include +#include + #include "database.h" HarshieDatabase::HarshieDatabase(const std::string& connectionString) { connection = PQconnectdb(connectionString.c_str()); + if (PQstatus(connection) != CONNECTION_OK) { + const auto errorLog = fmt::format("[{}]: Failed to connect to the database!", dpp::utility::current_date_time()); + fmt::print(errorLog); + throw std::runtime_error("Connection to the database failed: " + std::string(PQerrorMessage(connection))); } + else + fmt::print("[{}]: Connected to database!", dpp::utility::current_date_time()); } HarshieDatabase::~HarshieDatabase() diff --git a/src/cores/dotenv.cpp b/src/cores/dotenv.cpp index 54d4bd4..486e66d 100644 --- a/src/cores/dotenv.cpp +++ b/src/cores/dotenv.cpp @@ -20,88 +20,35 @@ void HarshieDotenv::load(const std::string& filename) { std::ifstream file(filename); - - if (!file.is_open()) - throw std::runtime_error("Failed to open .env file"); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << filename << std::endl; + return; + } std::string line; - while (std::getline(file, line)) - { - if (isComment(line)) - continue; - - expandVariables(line); - parseLine(line); - } + while (std::getline(file, line)) { + if (line.empty() || line[0] == '#') + continue; - file.close(); -} - -bool HarshieDotenv::has(const std::string& key) const -{ - return variables_.find(key) != variables_.end(); -} + size_t pos = line.find('='); + if (pos == std::string::npos) + continue; -std::string HarshieDotenv::get(const std::string& key) const -{ - if (!has(key)) - throw std::runtime_error("Undefined key: " + key); + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1); - return variables_.at(key); -} + if (!value.empty() && value[0] == '"' && value[value.size() - 1] == '"') + value = value.substr(1, value.size() - 2); -bool HarshieDotenv::isComment(const std::string& line) -{ - return line.find("#") == 0; + m_data[key] = value; + } } -void HarshieDotenv::expandVariables(std::string& line) -{ - std::size_t pos = 0; - - while ((pos = line.find('$', pos)) != std::string::npos) - { - if (pos + 1 < line.length()) - { - if (line[pos + 1] == '\\') - { - line.erase(pos, 1); - ++pos; - } - else if (line[pos + 1] == '{') - { - std::size_t endPos = line.find('}', pos + 2); - - if (endPos != std::string::npos) - { - std::string varName = line.substr(pos + 2, endPos - pos - 2); - std::string varValue = (has(varName)) ? get(varName) : ""; - - line.replace(pos, endPos - pos + 1, varValue); - pos += varValue.length(); - } - else - ++pos; - } - else - { - std::string varName = line.substr(pos + 1); - std::string varValue = (has(varName)) ? get(varName) : ""; - - line.replace(pos, varName.length() + 1, varValue); - pos += varValue.length(); - } - } - else - break; - } -} - -void HarshieDotenv::parseLine(const std::string& line) +std::string HarshieDotenv::get(const std::string& key) const { - std::istringstream iss(line); - std::string key, value; - - if (std::getline(iss >> std::ws, key, '=') && std::getline(iss >> std::ws, value)) - variables_[key] = value; + auto it = m_data.find(key); + if (it != m_data.end()) { + return it->second; + } + return ""; } diff --git a/src/cores/dotenv.h b/src/cores/dotenv.h index aebca4f..6b5620c 100644 --- a/src/cores/dotenv.h +++ b/src/cores/dotenv.h @@ -27,13 +27,8 @@ class HarshieDotenv { public: void load(const std::string& filename); - bool has(const std::string& key) const; std::string get(const std::string& key) const; private: - std::unordered_map variables_; - - bool isComment(const std::string& line); - void expandVariables(std::string& line); - void parseLine(const std::string& line); + std::unordered_map m_data; }; diff --git a/src/cores/harshie.cpp b/src/cores/harshie.cpp index 0e3f07a..c00c172 100644 --- a/src/cores/harshie.cpp +++ b/src/cores/harshie.cpp @@ -21,9 +21,11 @@ Harshie::Harshie() { - HarshieDotenv dotenv; dotenv.load(".env"); +} +void Harshie::HarshieStart() +{ const auto encodedToken = dotenv.get("ENCODED_CLIENT_TOKEN"); const auto token = HarshieDecoder::Decode(encodedToken); @@ -32,10 +34,8 @@ Harshie::Harshie() HarshieOnReady(); HarshieOnSlashCmnd(); -} - -void Harshie::HarshieStart() -{ + HarshieOnDatabaseConnect(); + client->start(dpp::st_wait); } @@ -85,3 +85,20 @@ void Harshie::HarshieOnSlashCmnd() iterator->second.function(*client, event); }); } + +void Harshie::HarshieOnDatabaseConnect() +{ + const auto databaseName = dotenv.get("DBNAME"); + const auto databaseHost = dotenv.get("DBHOST"); + const auto databasePort = dotenv.get("DBPORT"); + const auto databaseUser = dotenv.get("DBUSER"); + const auto databasePass = dotenv.get("DBPASS"); + const auto databaseAppName = dotenv.get("DBAPPNAME"); + + const auto databaseConnectStr = fmt::format( + "dbname={} host={} port={} user={} password={} application_name={}", + databaseName, databaseHost, databasePort, databaseUser, databasePass, databaseAppName + ); + + HarshieDatabase database(databaseConnectStr); +} diff --git a/src/cores/harshie.h b/src/cores/harshie.h index 28fd6fa..d96e36c 100644 --- a/src/cores/harshie.h +++ b/src/cores/harshie.h @@ -29,6 +29,7 @@ #include "dotenv.h" #include "decoder.h" +#include "database.h" class Harshie { public: @@ -37,10 +38,12 @@ class Harshie { void HarshieStart(); private: + HarshieDotenv dotenv; std::unique_ptr client; void HarshieOnReady(); void HarshieOnSlashCmnd(); + void HarshieOnDatabaseConnect(); void HarshieActivites(const dpp::ready_t& event); void HarshieRegisterSlashCmnd();