Add a 404 page
This commit is contained in:
		
							parent
							
								
									7f1cde5579
								
							
						
					
					
						commit
						80650fe6f1
					
				
							
								
								
									
										6
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										6
									
								
								main.cpp
								
								
								
								
							| 
						 | 
				
			
			@ -3,6 +3,7 @@
 | 
			
		|||
#include <nlohmann/json.hpp>
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
    if (req.has_header("X-Canonical-Origin")) {
 | 
			
		||||
        return req.get_header_value("X-Canonical-Origin");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,7 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <optional>
 | 
			
		||||
 | 
			
		||||
#include <httplib/httplib.h>
 | 
			
		||||
#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<std::string> subtitle = std::nullopt, std::optional<std::string> info = std::nullopt);
 | 
			
		||||
std::string get_origin(const httplib::Request& req, const Config& config);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue