From a808ec07cbbb4fc6118d97b8e7985debe1274a5f Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 19 May 2024 00:58:38 +0500 Subject: feat: use cfg to replace placeholders --- web/src/handlers.cpp | 7 ++++++- web/src/handlers.hpp | 6 +++++- 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 #include +#include #include +#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 &cfg) { std::shared_ptr parser = std::make_shared(); 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 + +#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 &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/") - ([](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; -- cgit v1.2.3