Skip to content

Commit

Permalink
[Fix]: dotenv issue
Browse files Browse the repository at this point in the history
  • Loading branch information
harshfeudal committed Sep 20, 2023
1 parent 0d8bfb8 commit 606ea9e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 87 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
ENCODED_CLIENT_TOKEN=ADD_HERE
ENCODED_CLIENT_TOKEN=ADD_HERE

DBNAME=ADD_HERE
DBHOST=ADD_HERE
DBPORT=ADD_HERE
DBUSER=ADD_HERE
DBPASS=ADD_HERE
DPAPPNAME=ADD_HERE
89 changes: 89 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions src/cores/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@
#include <stdexcept>
#include <cstdlib>

#include <spdlog/spdlog.h>
#include <dpp/dpp.h>

#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()
Expand Down
97 changes: 22 additions & 75 deletions src/cores/dotenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
7 changes: 1 addition & 6 deletions src/cores/dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> variables_;

bool isComment(const std::string& line);
void expandVariables(std::string& line);
void parseLine(const std::string& line);
std::unordered_map<std::string, std::string> m_data;
};
27 changes: 22 additions & 5 deletions src/cores/harshie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -32,10 +34,8 @@ Harshie::Harshie()

HarshieOnReady();
HarshieOnSlashCmnd();
}

void Harshie::HarshieStart()
{
HarshieOnDatabaseConnect();

client->start(dpp::st_wait);
}

Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/cores/harshie.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "dotenv.h"
#include "decoder.h"
#include "database.h"

class Harshie {
public:
Expand All @@ -37,10 +38,12 @@ class Harshie {
void HarshieStart();

private:
HarshieDotenv dotenv;
std::unique_ptr<dpp::cluster> client;

void HarshieOnReady();
void HarshieOnSlashCmnd();
void HarshieOnDatabaseConnect();

void HarshieActivites(const dpp::ready_t& event);
void HarshieRegisterSlashCmnd();
Expand Down

0 comments on commit 606ea9e

Please sign in to comment.