From 6d44c020b3003b4a88f3b929df7dc978e6652acb Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Thu, 18 Apr 2024 11:24:33 +1000 Subject: [PATCH] made contact form functional --- .gitmodules | 3 +++ CMakeLists.txt | 2 +- matrix-commander | 1 + src/contact.cpp | 23 ++++++++++++++++++ src/contact.hpp | 13 +++++++++++ src/main.cpp | 41 ++++++++++++++++++++++----------- static/index.html | 2 +- static/public/css/style.css | 22 ++++++++++-------- static/public/html/contact.html | 2 +- 9 files changed, 83 insertions(+), 26 deletions(-) create mode 160000 matrix-commander create mode 100644 src/contact.cpp create mode 100644 src/contact.hpp diff --git a/.gitmodules b/.gitmodules index cf9f864..efafc10 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "cpp-httplib"] path = cpp-httplib url = https://github.com/yhirose/cpp-httplib +[submodule "matrix-commander"] + path = matrix-commander + url = https://github.com/8go/matrix-commander diff --git a/CMakeLists.txt b/CMakeLists.txt index 77b2b2e..e47107a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,5 @@ message("Using cmake flags: ${CMAKE_CXX_FLAGS}") file(GLOB_RECURSE SOURCES src/*.cpp) add_executable(Portfolio ${SOURCES}) -target_link_libraries(Portfolio PUBLIC stdc++) +target_link_libraries(Portfolio PUBLIC stdc++ jsoncpp) diff --git a/matrix-commander b/matrix-commander new file mode 160000 index 0000000..79a5d0c --- /dev/null +++ b/matrix-commander @@ -0,0 +1 @@ +Subproject commit 79a5d0c2db9b3c49de0d0538affbe07f1a266f84 diff --git a/src/contact.cpp b/src/contact.cpp new file mode 100644 index 0000000..90f19d6 --- /dev/null +++ b/src/contact.cpp @@ -0,0 +1,23 @@ + +#include "contact.hpp" +#include +#include + +bool Contact::init() +{ + return true; +} + +bool Contact::send(const std::string &name, const std::string &email, const std::string &message) +{ + int id = fork(); + + if(!id) + { + std::string body = std::format("Message: {0} ({1})\n\n{2}", name, email, message); + execlp("../matrix-commander/venv/bin/python3", "../matrix-commander/venv/bin/python3", "../matrix-commander/matrix_commander/matrix-commander", "-m", body.c_str(), nullptr); + } + + return true; +} + diff --git a/src/contact.hpp b/src/contact.hpp new file mode 100644 index 0000000..19160d1 --- /dev/null +++ b/src/contact.hpp @@ -0,0 +1,13 @@ + +#pragma once + +#include + +namespace Contact +{ + +bool init(); +bool send(const std::string& name, const std::string& email, const std::string& message); + +}; + diff --git a/src/main.cpp b/src/main.cpp index e0e0379..0780e97 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include "../cpp-httplib/httplib.h" + #include "files.hpp" +#include "contact.hpp" #include #include @@ -8,14 +10,13 @@ #include #include -std::string data_index = Files::load("../static/index.html"); -std::string data_error = Files::load("../static/error.html"); - -httplib::Server svr; - +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"); @@ -31,7 +32,7 @@ struct page_handler void operator()(const httplib::Request& req, httplib::Response& res) { std::string data_content = Files::load(path); - std::string page = std::vformat(data_index, std::make_format_args(get_theme(req), data_content)); + std::string page = std::vformat(DATA_INDEX, std::make_format_args(get_theme(req), data_content)); res.set_content(page, "text/html"); } }; @@ -50,24 +51,38 @@ struct theme_handler } }; -void post_contact(const httplib::Request& req, httplib::Response& res) +void put_contact(const httplib::Request& req, httplib::Response& res) { - std::cout << "Contact form submitted\n"; - std::cout << "Name: " << req.get_param_value("name") << "\n"; - std::cout << "Email: " << req.get_param_value("email") << "\n"; - std::cout << "Message: " << req.get_param_value("message") << "\n"; + 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 page = std::vformat(data_index, std::make_format_args(get_theme(req), data_error)); + std::string page = std::vformat(DATA_INDEX, std::make_format_args(get_theme(req), 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")); @@ -75,7 +90,7 @@ int main() svr.Get("/contact", page_handler("../static/public/html/contact.html")); svr.Get("/about", page_handler("../static/public/html/about.html")); - svr.Post("/contact", post_contact); + svr.Put("/contact", put_contact); svr.set_error_handler(error_handler); diff --git a/static/index.html b/static/index.html index 687ac48..0e77517 100644 --- a/static/index.html +++ b/static/index.html @@ -14,7 +14,7 @@
  • -
  • +
  • diff --git a/static/public/css/style.css b/static/public/css/style.css index 06dea03..e8dc1b7 100644 --- a/static/public/css/style.css +++ b/static/public/css/style.css @@ -1,4 +1,8 @@ +p, label, th, td, li { + font-size: 16px; +} + .button-link { background: none; border: none; @@ -10,7 +14,7 @@ img { width: calc(min(100%, 800px)); - border-radius: 0.25em; + border-radius: 4px; border-style: none; } @@ -54,11 +58,11 @@ ul.navbar li { padding: 0; } -ul.navbar li button { +ul.navbar li button, ul.navbar li .button-link { transition: 0.25s; background-color: inherit; border: none; - padding: 1em 1.5em; + padding: 16px 24px; font: inherit; cursor: pointer; text-decoration: none; @@ -69,11 +73,9 @@ ul.navbar li.right { float: right; } -ul.navbar li.right button { - font-size: 1.5em; - padding: 0; - height: 54px; - width: 54px; +ul.navbar li .button-emoji { + font-size: 30px; + padding: 6px 12px; } input[type="text"], input[type="email"], textarea { @@ -82,11 +84,11 @@ input[type="text"], input[type="email"], textarea { } input[type="text"], input[type="email"], input[type="button"], textarea, button { - border-radius: 0.25em; + border-radius: 4px; border-width: thin; border-style: solid; border-color: gray; - padding: 0.5em 0.75em; + padding: 8px 12px; } textarea { diff --git a/static/public/html/contact.html b/static/public/html/contact.html index d664ebf..d6c6bbd 100644 --- a/static/public/html/contact.html +++ b/static/public/html/contact.html @@ -6,7 +6,7 @@

    You can contact me via this form or by emailing me at jsrobson10@gmail.com. -

    +