Move controller code to src/controller/ and out of src/main.c, add extremely simple 404 error page

master
teknomunk 1 year ago
parent f27aadd39c
commit 1aed804578

@ -0,0 +1,42 @@
#include "http_request.h"
#include "controller/mastodon_api.h"
bool route_asset( struct http_request* req )
{
static struct allowed_t {
const char* route_path;
const char* fs_path;
const char* mime_type;
} allowed[] = {
{ "style.css", "src/assets/style.css", "text/css" },
{ "jquery.js", "src/assets/jquery.js", "application/javascript" },
{ "jquery.plugins.js", "src/assets/jquery.plugins.js", "application/javascript" },
{ "background_noise.png","src/assets/background_noise.png", "image/png" },
{ "feed-icon-28x28.png", "src/assets/feed-icon-28x28.png", "image/png" },
};
for( int i = 0; i < sizeof(allowed)/sizeof(struct allowed_t); ++i ) {
if( http_request_route_term( req, allowed[i].route_path ) ) {
return http_request_send_file( req, allowed[i].fs_path, allowed[i].mime_type );
}
}
return false;
}
bool route_request( struct http_request* req )
{
if( http_request_route( req, "/api/v1/" ) ) {
return route_mastodon_api( req );
}
if( http_request_route( req, "/.well-known" ) ) {
return false;
}
if( http_request_route( req, "/assets/" ) ) {
printf( "handling asset\n" );
return route_asset(req);
}
return false;
}

@ -0,0 +1,6 @@
#pragma once
struct http_request;
bool route_request( struct http_request* req );

@ -9,7 +9,7 @@
#include "http_server.h"
#include "http_request.h"
#include "controller/mastodon_api.h"
#include "controller/main.h"
bool terminate = false;
@ -18,51 +18,16 @@ void handle_ctrl_c(int)
terminate = true;
}
bool route_asset( struct http_request* req )
{
static struct allowed_t {
const char* route_path;
const char* fs_path;
const char* mime_type;
} allowed[] = {
{ "style.css", "src/assets/style.css", "text/css" },
{ "jquery.js", "src/assets/jquery.js", "application/javascript" },
{ "jquery.plugins.js", "src/assets/jquery.plugins.js", "application/javascript" },
{ "background_noise.png","src/assets/background_noise.png", "image/png" },
{ "feed-icon-28x28.png", "src/assets/feed-icon-28x28.png", "image/png" },
};
for( int i = 0; i < sizeof(allowed)/sizeof(struct allowed_t); ++i ) {
if( http_request_route_term( req, allowed[i].route_path ) ) {
return http_request_send_file( req, allowed[i].fs_path, allowed[i].mime_type );
}
}
return false;
}
bool route_request( struct http_request* req )
{
if( http_request_route( req, "/api/v1/" ) ) {
return route_mastodon_api( req );
}
if( http_request_route( req, "/.well-known" ) ) {
return false;
}
if( http_request_route( req, "/assets/" ) ) {
printf( "handling asset\n" );
return route_asset(req);
}
return false;
}
void handle_request( struct http_request* req, void* )
{
printf( "Handling request from %s\n", http_request_get_remote_host_address( req ) );
if( !route_request( req ) ) {
FILE* f = http_request_get_response_body( req );
http_request_send_headers( req, 404, "text/html", true );
#define RENDER
#include "view/404.html.inc"
#undef RENDER
}
}

@ -0,0 +1 @@
Page not found.
Loading…
Cancel
Save