From a127a487156d46f319e10e327f053b7a9b57b757 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 8 May 2024 12:24:04 +1000 Subject: [PATCH] improve build system --- .clangd | 3 ++ src/CMakeLists.txt | 4 +- src/contact.cpp | 4 +- src/contact.hpp | 4 +- src/files.cpp | 2 +- src/files.hpp | 2 +- src/main.cpp | 91 +++-------------------------------- src/page/CMakeLists.txt | 3 ++ src/page/contact.cpp | 30 ++++++++++++ src/page/contact.hpp | 10 ++++ src/page/error_handler.cpp | 24 +++++++++ src/page/error_handler.hpp | 10 ++++ src/page/nav_targets.cpp | 32 ++++++++++++ src/page/nav_targets.hpp | 10 ++++ src/page/root.cpp | 8 +++ src/page/root.hpp | 10 ++++ src/page/theme.cpp | 14 ++++++ src/page/theme.hpp | 10 ++++ static/public/html/about.html | 2 +- 19 files changed, 180 insertions(+), 93 deletions(-) create mode 100644 .clangd create mode 100644 src/page/CMakeLists.txt create mode 100644 src/page/contact.cpp create mode 100644 src/page/contact.hpp create mode 100644 src/page/error_handler.cpp create mode 100644 src/page/error_handler.hpp create mode 100644 src/page/nav_targets.cpp create mode 100644 src/page/nav_targets.hpp create mode 100644 src/page/root.cpp create mode 100644 src/page/root.hpp create mode 100644 src/page/theme.cpp create mode 100644 src/page/theme.hpp diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..c75a991 --- /dev/null +++ b/.clangd @@ -0,0 +1,3 @@ + +CompileFlags: + Add: -std=c++20 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e9179d..74c56ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,5 @@ -file(GLOB SOURCES src/*.cpp) +include(src/page/CMakeLists.txt) + +file(GLOB SOURCES ${SOURCES} src/*.cpp) diff --git a/src/contact.cpp b/src/contact.cpp index 90f19d6..1661d63 100644 --- a/src/contact.cpp +++ b/src/contact.cpp @@ -3,12 +3,12 @@ #include #include -bool Contact::init() +bool Contact::Init() { return true; } -bool Contact::send(const std::string &name, const std::string &email, const std::string &message) +bool Contact::Send(const std::string &name, const std::string &email, const std::string &message) { int id = fork(); diff --git a/src/contact.hpp b/src/contact.hpp index 19160d1..7ac7db9 100644 --- a/src/contact.hpp +++ b/src/contact.hpp @@ -6,8 +6,8 @@ namespace Contact { -bool init(); -bool send(const std::string& name, const std::string& email, const std::string& message); +bool Init(); +bool Send(const std::string& name, const std::string& email, const std::string& message); }; diff --git a/src/files.cpp b/src/files.cpp index f35796a..bf3334e 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -3,7 +3,7 @@ #include #include -std::string Files::load(const std::string& path) +std::string Files::Load(const std::string& path) { std::ifstream file(path); std::stringstream ss; diff --git a/src/files.hpp b/src/files.hpp index 8d5d4f6..38cd9de 100644 --- a/src/files.hpp +++ b/src/files.hpp @@ -5,6 +5,6 @@ namespace Files { - std::string load(const std::string& path); + std::string Load(const std::string& path); } diff --git a/src/main.cpp b/src/main.cpp index bdd97d5..a2e31ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,100 +1,21 @@ #include "cpp-httplib/httplib.h" +#include "page/contact.hpp" +#include "page/error_handler.hpp" +#include "page/nav_targets.hpp" -#include "files.hpp" -#include "contact.hpp" - -#include #include #include -#include -#include - -const std::string DATA_INDEX = Files::load("../static/index.html"); -const std::string DATA_ERROR = Files::load("../static/error.html"); -const std::string PATH_LIGHT = "/static/css/light.css"; -const std::string PATH_DARK = "/static/css/dark.css"; httplib::Server svr; -std::string get_theme(const httplib::Request& req) -{ - std::string cookies = req.get_header_value("Cookie"); - return regex_search(cookies, std::regex("(^|;) *theme=light")) ? PATH_LIGHT : PATH_DARK; -} - -struct page_handler -{ - std::string path; - - page_handler(std::string path) : path(path) {} - - void operator()(const httplib::Request& req, httplib::Response& res) - { - std::string data_content = Files::load(path); - std::string theme = get_theme(req); - std::string page = std::vformat(DATA_INDEX, std::make_format_args(theme, data_content)); - res.set_content(page, "text/html"); - } -}; - -struct theme_handler -{ - std::string theme; - std::string content; - - theme_handler(std::string theme, std::string content) : theme(theme), content(content) {} - - void operator()(const httplib::Request& req, httplib::Response& res) - { - res.set_header("Set-Cookie", std::format("theme={}; Path=/; SameSite=Strict; HttpOnly; Max-Age=31536000", theme)); - res.set_content(content, "text/html"); - } -}; - -void put_contact(const httplib::Request& req, httplib::Response& res) -{ - if(!req.has_param("name") || !req.has_param("email") || !req.has_param("message")) - { - res.status = 400; - res.set_content("

Bad request

", "text/html"); - return; - } - - if(!Contact::send(req.get_param_value("name"), req.get_param_value("email"), req.get_param_value("message"))) - { - res.status = 500; - res.set_content("

Internal server error

", "text/html"); - return; - } - - res.set_content("

Message sent. Thanks!

", "text/html"); -} - -void error_handler(const httplib::Request& req, httplib::Response& res) -{ - std::string theme = get_theme(req); - std::string page = std::vformat(DATA_INDEX, std::make_format_args(theme, DATA_ERROR)); - res.set_content(page, "text/html"); -} - int main() { - if(!Contact::init()) - { - return 1; - } - svr.set_mount_point("/static", "../static/public/"); - svr.Get("/", page_handler("../static/public/html/projects.html")); - svr.Get("/projects", page_handler("../static/public/html/projects.html")); - svr.Get("/contact", page_handler("../static/public/html/contact.html")); - svr.Get("/about", page_handler("../static/public/html/about.html")); - - svr.Put("/contact", put_contact); - - svr.set_error_handler(error_handler); + Page::NavTargets::Init(svr); + Page::Contact::Init(svr); + Page::ErrorHandler::Init(svr); std::cout << "Listening on port 8080\n"; svr.listen("0.0.0.0", 8080); diff --git a/src/page/CMakeLists.txt b/src/page/CMakeLists.txt new file mode 100644 index 0000000..7bb905c --- /dev/null +++ b/src/page/CMakeLists.txt @@ -0,0 +1,3 @@ + +file(GLOB SOURCES ${SOURCES} src/page/*.cpp) + diff --git a/src/page/contact.cpp b/src/page/contact.cpp new file mode 100644 index 0000000..5d1111d --- /dev/null +++ b/src/page/contact.cpp @@ -0,0 +1,30 @@ + +#include "contact.hpp" +#include "../contact.hpp" + +void put_contact(const httplib::Request& req, httplib::Response& res) +{ + if(!req.has_param("name") || !req.has_param("email") || !req.has_param("message")) + { + res.status = 400; + res.set_content("

Bad request

", "text/html"); + return; + } + + if(!::Contact::Send(req.get_param_value("name"), req.get_param_value("email"), req.get_param_value("message"))) + { + res.status = 500; + res.set_content("

Internal server error

", "text/html"); + return; + } + + res.set_content("

Message sent. Thanks!

", "text/html"); +} + +void Page::Contact::Init(httplib::Server &svr) +{ + ::Contact::Init(); + + svr.Put("/contact", put_contact); +} + diff --git a/src/page/contact.hpp b/src/page/contact.hpp new file mode 100644 index 0000000..6299afd --- /dev/null +++ b/src/page/contact.hpp @@ -0,0 +1,10 @@ + +#pragma once + +#include "../cpp-httplib/httplib.h" + +namespace Page::Contact +{ + void Init(httplib::Server& svr); +}; + diff --git a/src/page/error_handler.cpp b/src/page/error_handler.cpp new file mode 100644 index 0000000..7c1ce33 --- /dev/null +++ b/src/page/error_handler.cpp @@ -0,0 +1,24 @@ + +#include "../files.hpp" +#include "error_handler.hpp" +#include "root.hpp" +#include "theme.hpp" + +#include + +using namespace Page; + +static const std::string DATA_ERROR = Files::Load("../static/error.html"); + +void error_handler(const httplib::Request& req, httplib::Response& res) +{ + std::string theme = Theme::Get(req); + std::string page = std::vformat(Root::DATA_INDEX, std::make_format_args(theme, DATA_ERROR)); + res.set_content(page, "text/html"); +} + +void Page::ErrorHandler::Init(httplib::Server &svr) +{ + svr.set_error_handler(error_handler); +} + diff --git a/src/page/error_handler.hpp b/src/page/error_handler.hpp new file mode 100644 index 0000000..95d74ab --- /dev/null +++ b/src/page/error_handler.hpp @@ -0,0 +1,10 @@ + +#pragma once + +#include "../cpp-httplib/httplib.h" + +namespace Page::ErrorHandler +{ + void Init(httplib::Server& svr); +}; + diff --git a/src/page/nav_targets.cpp b/src/page/nav_targets.cpp new file mode 100644 index 0000000..fee57e7 --- /dev/null +++ b/src/page/nav_targets.cpp @@ -0,0 +1,32 @@ + +#include "nav_targets.hpp" +#include "root.hpp" +#include "theme.hpp" +#include "../files.hpp" +#include + +using namespace Page; + +struct page_handler +{ + std::string path; + + page_handler(std::string path) : path(path) {} + + void operator()(const httplib::Request& req, httplib::Response& res) + { + std::string data_content = Files::Load(path); + std::string theme = Theme::Get(req); + std::string page = std::vformat(Root::DATA_INDEX, std::make_format_args(theme, data_content)); + res.set_content(page, "text/html"); + } +}; + +void NavTargets::Init(httplib::Server &svr) +{ + svr.Get("/", page_handler("../static/public/html/projects.html")); + svr.Get("/projects", page_handler("../static/public/html/projects.html")); + svr.Get("/contact", page_handler("../static/public/html/contact.html")); + svr.Get("/about", page_handler("../static/public/html/about.html")); +} + diff --git a/src/page/nav_targets.hpp b/src/page/nav_targets.hpp new file mode 100644 index 0000000..c98b311 --- /dev/null +++ b/src/page/nav_targets.hpp @@ -0,0 +1,10 @@ + +#pragma once + +#include "../cpp-httplib/httplib.h" + +namespace Page::NavTargets +{ + void Init(httplib::Server& svr); +}; + diff --git a/src/page/root.cpp b/src/page/root.cpp new file mode 100644 index 0000000..7f29913 --- /dev/null +++ b/src/page/root.cpp @@ -0,0 +1,8 @@ + +#include "root.hpp" +#include "../files.hpp" + +using namespace Page; + +const std::string Root::DATA_INDEX = Files::Load("../static/index.html"); + diff --git a/src/page/root.hpp b/src/page/root.hpp new file mode 100644 index 0000000..b9b4c88 --- /dev/null +++ b/src/page/root.hpp @@ -0,0 +1,10 @@ + +#pragma once + +#include + +namespace Page::Root +{ + const extern std::string DATA_INDEX; +}; + diff --git a/src/page/theme.cpp b/src/page/theme.cpp new file mode 100644 index 0000000..3a7c9ee --- /dev/null +++ b/src/page/theme.cpp @@ -0,0 +1,14 @@ + +#include "theme.hpp" + +static const std::string PATH_LIGHT = "/static/css/light.css"; +static const std::string PATH_DARK = "/static/css/dark.css"; + +using namespace Page; + +std::string Theme::Get(const httplib::Request& req) +{ + std::string cookies = req.get_header_value("Cookie"); + return regex_search(cookies, std::regex("(^|;) *theme=light")) ? PATH_LIGHT : PATH_DARK; +} + diff --git a/src/page/theme.hpp b/src/page/theme.hpp new file mode 100644 index 0000000..17f225d --- /dev/null +++ b/src/page/theme.hpp @@ -0,0 +1,10 @@ + +#pragma once + +#include "../cpp-httplib/httplib.h" + +namespace Page::Theme +{ + std::string Get(const httplib::Request& req); +}; + diff --git a/static/public/html/about.html b/static/public/html/about.html index 1656bcd..2b3850b 100644 --- a/static/public/html/about.html +++ b/static/public/html/about.html @@ -1,7 +1,7 @@ Jays Portfolio - About -

About Me

+

About Me