From 80650fe6f1a4f8c242b182afe6d4d7ff430551c4 Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 3 Apr 2023 22:27:07 +0700 Subject: [PATCH] Add a 404 page --- main.cpp | 6 ++++++ routes/css.cpp | 13 +++++++++++++ servehelper.cpp | 19 +++++++++++++++++++ servehelper.h | 4 ++++ 4 files changed, 42 insertions(+) diff --git a/main.cpp b/main.cpp index 41d01af..08ffc68 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include "config.h" +#include "servehelper.h" #include "routes/routes.h" int main(int argc, char** argv) { @@ -25,6 +26,11 @@ int main(int argc, char** argv) { }); server.Get("/style.css", css_route); + server.Get(".*", [&](const httplib::Request& req, httplib::Response& res) { + res.status = 404; + serve_error(req, res, config, "404: Page not found"); + }); + if (config.bind_port != 0) { if (!server.bind_to_port(config.bind_host, config.bind_port)) { fprintf(stderr, "Failed to bind to %s:%d\n", config.bind_host.c_str(), config.bind_port); diff --git a/routes/css.cpp b/routes/css.cpp index 08dc066..9a00191 100644 --- a/routes/css.cpp +++ b/routes/css.cpp @@ -6,6 +6,11 @@ void css_route(const httplib::Request& req, httplib::Response& res) { :root { --background-color: black; --text-color: white; + + --error-background-color: rgb(100, 0, 0); + --error-border-color: red; + --error-text-color: white; + --accent-color: #962AC3; --dark-accent-color: #7D3F7D; --bright-accent-color: #DE6DE6; @@ -25,5 +30,13 @@ void css_route(const httplib::Request& req, httplib::Response& res) { text-decoration: underline; } + .error { + text-align: center; + background-color: var(--error-background-color); + color: var(--error-text-color); + border-style: solid; + border-color: var(--error-border-color); + } + )EOF", "text/css"); } diff --git a/servehelper.cpp b/servehelper.cpp index 30dc6fe..459a5b5 100644 --- a/servehelper.cpp +++ b/servehelper.cpp @@ -19,6 +19,25 @@ void serve(const httplib::Request& req, httplib::Response& res, const Config& co res.set_content(""s + html.serialize(), "text/html"); } +void serve_error(const httplib::Request& req, httplib::Response& res, const Config& config, + std::string title, std::optional subtitle, std::optional info) { + + Element error_div("div", {{"class", "error"}}, { + Element("h2", {title}) + }); + if (subtitle) { + error_div.nodes.push_back(std::move(*subtitle)); + } + if (info) { + error_div.nodes.push_back(Element("pre", { + Element("code", {std::move(*info)}) + })); + } + + Element body("body", {std::move(error_div)}); + serve(req, res, config, std::move(title), std::move(body)); +} + std::string get_origin(const httplib::Request& req, const Config& config) { if (req.has_header("X-Canonical-Origin")) { return req.get_header_value("X-Canonical-Origin"); diff --git a/servehelper.h b/servehelper.h index ea5aef5..50893ab 100644 --- a/servehelper.h +++ b/servehelper.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "blankie/serializer.h" @@ -7,4 +9,6 @@ struct Config; // forward declaration from config.h using Element = blankie::html::Element; void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element); +void serve_error(const httplib::Request& req, httplib::Response& res, const Config& config, + std::string title, std::optional subtitle = std::nullopt, std::optional info = std::nullopt); std::string get_origin(const httplib::Request& req, const Config& config);