Keep all baked assets in one place
This commit is contained in:
76
routes.c
76
routes.c
@ -2,8 +2,32 @@
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
#include "routes.h"
|
||||
#include "gpp.h"
|
||||
#include "tmpls.h"
|
||||
#include "baked.h"
|
||||
|
||||
#define INTERNAL_SERVER_ERROR_MSG "Internal server error ;(. Try again later..."
|
||||
|
||||
bool gpp_run(char *path, NString_List *env, String_Builder *out)
|
||||
{
|
||||
Cmd cmd = {0};
|
||||
defer { cmd_free(&cmd); }
|
||||
|
||||
char gpp1[PATH_MAX];
|
||||
if (!get_baked_resource_path("gpp1", gpp1, sizeof(gpp1))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cmd_append(&cmd, gpp1);
|
||||
cmd_append(&cmd, "-H");
|
||||
cmd_append(&cmd, "-x");
|
||||
cmd_append(&cmd, "--nostdinc");
|
||||
cmd_append(&cmd, path);
|
||||
|
||||
for (size_t i = 0; i < env->count; i++) {
|
||||
cmd_append(&cmd, env->items[i]);
|
||||
}
|
||||
|
||||
return cmd_run_collect(&cmd, out) == 0;
|
||||
}
|
||||
|
||||
void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
{
|
||||
@ -16,13 +40,16 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
|
||||
defer { sb_free(&out); }
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
tmpl_get_path_by_key(TMPL_PAGE_MISSING, path, sizeof(path));
|
||||
if (!get_baked_resource_path("page-missing.t", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = gpp_run(path, &env, &out);
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;(");
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
} else {
|
||||
mg_http_reply(conn, 404, "Content-Type: text/html\r\n", out.items);
|
||||
}
|
||||
@ -30,14 +57,40 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
|
||||
|
||||
void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
{
|
||||
struct mg_http_serve_opts opts = { 0 };
|
||||
mg_http_serve_file(conn, msg, "./assets/simple.min.css", &opts);
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("simple.min.css", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: text/css\r\n", "%s", sb.items);
|
||||
}
|
||||
|
||||
void route_favicon(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
{
|
||||
struct mg_http_serve_opts opts = { 0 };
|
||||
mg_http_serve_file(conn, msg, "./assets/favicon.ico", &opts);
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("favicon.ico", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: image/x-icon\r\n", "%s", sb.items);
|
||||
}
|
||||
|
||||
void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
@ -49,13 +102,16 @@ void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
defer { sb_free(&out); }
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
tmpl_get_path_by_key(TMPL_HOME, path, sizeof(path));
|
||||
if (!get_baked_resource_path("home.t", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = gpp_run(path, &env, &out);
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;(");
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
} else {
|
||||
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
||||
}
|
||||
|
Reference in New Issue
Block a user