diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-19 00:58:38 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-19 00:58:38 +0500 |
| commit | a808ec07cbbb4fc6118d97b8e7985debe1274a5f (patch) | |
| tree | 4d9ce401fac0265018274db20531f73fe612fa8e /web | |
| parent | feede6c8024147d0b2f12c18d7dca622a797645b (diff) | |
feat: use cfg to replace placeholders
Diffstat (limited to 'web')
| -rw-r--r-- | web/src/handlers.cpp | 7 | ||||
| -rw-r--r-- | web/src/handlers.hpp | 6 | ||||
| -rw-r--r-- | web/src/main.cpp | 15 |
3 files changed, 22 insertions, 6 deletions
diff --git a/web/src/handlers.cpp b/web/src/handlers.cpp index 1dfeb81..03cddd9 100644 --- a/web/src/handlers.cpp +++ b/web/src/handlers.cpp @@ -2,14 +2,17 @@ #include <fstream> #include <memory> +#include <optional> #include <string> +#include "config.hpp" #include "crow/http_response.h" #include "crow/mustache.h" #include "maddy/parser.h" namespace botweb { - crow::response get_wiki_page(const std::string &path) { + crow::response get_wiki_page(const std::string &path, + const std::optional<Configuration> &cfg) { std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(); std::ifstream contents("docs/" + path + ".md"); @@ -25,6 +28,8 @@ namespace botweb { crow::mustache::context ctx; ctx["content"] = contents_html; ctx["summary"] = summary_html; + ctx["contact_url"] = (*cfg).contact_url; + ctx["contact_name"] = (*cfg).contact_name; return crow::response(200, page.render(ctx)); } diff --git a/web/src/handlers.hpp b/web/src/handlers.hpp index d150e6d..5321a46 100644 --- a/web/src/handlers.hpp +++ b/web/src/handlers.hpp @@ -1,7 +1,11 @@ #pragma once +#include <optional> + +#include "config.hpp" #include "crow/http_response.h" namespace botweb { - crow::response get_wiki_page(const std::string &path); + crow::response get_wiki_page(const std::string &path, + const std::optional<Configuration> &cfg); } diff --git a/web/src/main.cpp b/web/src/main.cpp index b63151c..314d2bf 100644 --- a/web/src/main.cpp +++ b/web/src/main.cpp @@ -9,15 +9,22 @@ int main(int argc, char *argv[]) { crow::SimpleApp app; CROW_ROUTE(app, "/") - ([]() { + ([&cfg]() { auto page = crow::mustache::load("index.html"); - return page.render(); + crow::mustache::context ctx; + ctx["contact_url"] = (*cfg).contact_url; + ctx["contact_name"] = (*cfg).contact_name; + + return page.render(ctx); }); - CROW_ROUTE(app, "/wiki")([]() { return botweb::get_wiki_page("/README"); }); + CROW_ROUTE(app, "/wiki") + ([&cfg]() { return botweb::get_wiki_page("/README", cfg); }); CROW_ROUTE(app, "/wiki/<path>") - ([](const std::string &path) { return botweb::get_wiki_page(path); }); + ([&cfg](const std::string &path) { + return botweb::get_wiki_page(path, cfg); + }); app.multithreaded().port(18083).run(); return 0; |
