summaryrefslogtreecommitdiff
path: root/web/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-19 00:58:08 +0500
committerilotterytea <iltsu@alright.party>2024-05-19 00:58:08 +0500
commitfeede6c8024147d0b2f12c18d7dca622a797645b (patch)
tree4e353f8fc1bb396a2a2cf640e9f33775538abb02 /web/src
parent8539ae8ed393371270ac269d0f0069f1b25f6f6f (diff)
feat: config
Diffstat (limited to 'web/src')
-rw-r--r--web/src/config.cpp46
-rw-r--r--web/src/config.hpp12
-rw-r--r--web/src/main.cpp3
3 files changed, 61 insertions, 0 deletions
diff --git a/web/src/config.cpp b/web/src/config.cpp
new file mode 100644
index 0000000..dbeb01e
--- /dev/null
+++ b/web/src/config.cpp
@@ -0,0 +1,46 @@
+#include "config.hpp"
+
+#include <fstream>
+#include <sstream>
+
+#include "crow/logging.h"
+
+namespace botweb {
+ std::optional<Configuration> parse_configuration_from_file(
+ const std::string &file_path) {
+ std::ifstream ifs(file_path);
+
+ if (!ifs.is_open()) {
+ CROW_LOG_ERROR << "Failed to open the configuration file at "
+ << file_path;
+ return std::nullopt;
+ }
+
+ Configuration cfg;
+
+ std::string line;
+ while (std::getline(ifs, line, '\n')) {
+ std::istringstream iss(line);
+ std::string key;
+ std::string value;
+
+ std::getline(iss, key, '=');
+ std::getline(iss, value);
+
+ for (char &c : key) {
+ c = tolower(c);
+ }
+
+ if (key == "contact_name") {
+ cfg.contact_name = value;
+ } else if (key == "contact_url") {
+ cfg.contact_url = value;
+ }
+ }
+
+ CROW_LOG_INFO << "Successfully loaded the configuration from " << file_path
+ << "'";
+
+ return cfg;
+ }
+}
diff --git a/web/src/config.hpp b/web/src/config.hpp
new file mode 100644
index 0000000..11f8b04
--- /dev/null
+++ b/web/src/config.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <optional>
+#include <string>
+namespace botweb {
+ struct Configuration {
+ std::string contact_name = "some guy", contact_url = "#";
+ };
+
+ std::optional<Configuration> parse_configuration_from_file(
+ const std::string &file_path);
+}
diff --git a/web/src/main.cpp b/web/src/main.cpp
index 06936c5..b63151c 100644
--- a/web/src/main.cpp
+++ b/web/src/main.cpp
@@ -1,8 +1,11 @@
+#include "config.hpp"
#include "crow/app.h"
#include "crow/mustache.h"
#include "handlers.hpp"
int main(int argc, char *argv[]) {
+ auto cfg = botweb::parse_configuration_from_file(".env");
+
crow::SimpleApp app;
CROW_ROUTE(app, "/")