Add a 404 page

This commit is contained in:
blankie 2023-04-03 22:27:07 +07:00
parent 7f1cde5579
commit 80650fe6f1
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 42 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "config.h" #include "config.h"
#include "servehelper.h"
#include "routes/routes.h" #include "routes/routes.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -25,6 +26,11 @@ int main(int argc, char** argv) {
}); });
server.Get("/style.css", css_route); 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 (config.bind_port != 0) {
if (!server.bind_to_port(config.bind_host, config.bind_port)) { 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); fprintf(stderr, "Failed to bind to %s:%d\n", config.bind_host.c_str(), config.bind_port);

View File

@ -6,6 +6,11 @@ void css_route(const httplib::Request& req, httplib::Response& res) {
:root { :root {
--background-color: black; --background-color: black;
--text-color: white; --text-color: white;
--error-background-color: rgb(100, 0, 0);
--error-border-color: red;
--error-text-color: white;
--accent-color: #962AC3; --accent-color: #962AC3;
--dark-accent-color: #7D3F7D; --dark-accent-color: #7D3F7D;
--bright-accent-color: #DE6DE6; --bright-accent-color: #DE6DE6;
@ -25,5 +30,13 @@ void css_route(const httplib::Request& req, httplib::Response& res) {
text-decoration: underline; 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"); )EOF", "text/css");
} }

View File

@ -19,6 +19,25 @@ void serve(const httplib::Request& req, httplib::Response& res, const Config& co
res.set_content("<!DOCTYPE html>"s + html.serialize(), "text/html"); res.set_content("<!DOCTYPE html>"s + html.serialize(), "text/html");
} }
void serve_error(const httplib::Request& req, httplib::Response& res, const Config& config,
std::string title, std::optional<std::string> subtitle, std::optional<std::string> 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) { std::string get_origin(const httplib::Request& req, const Config& config) {
if (req.has_header("X-Canonical-Origin")) { if (req.has_header("X-Canonical-Origin")) {
return req.get_header_value("X-Canonical-Origin"); return req.get_header_value("X-Canonical-Origin");

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <optional>
#include <httplib/httplib.h> #include <httplib/httplib.h>
#include "blankie/serializer.h" #include "blankie/serializer.h"
@ -7,4 +9,6 @@ struct Config; // forward declaration from config.h
using Element = blankie::html::Element; using Element = blankie::html::Element;
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element 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<std::string> subtitle = std::nullopt, std::optional<std::string> info = std::nullopt);
std::string get_origin(const httplib::Request& req, const Config& config); std::string get_origin(const httplib::Request& req, const Config& config);