From fbe749a13eabe4e1c9cb67226745511a162445ee Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 10 Jun 2025 00:19:02 +0200 Subject: [PATCH] Keep all baked assets in one place --- baked.c | 56 +++++++++++++++++++++++++++++++++++++---- baked.h | 18 +++++++++++--- build.c | 12 ++++----- gpp.c | 43 -------------------------------- gpp.h | 12 --------- main.c | 9 +++---- routes.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-------- tmpls.c | 46 ---------------------------------- tmpls.h | 18 -------------- 9 files changed, 140 insertions(+), 150 deletions(-) delete mode 100644 gpp.c delete mode 100644 gpp.h delete mode 100644 tmpls.c delete mode 100644 tmpls.h diff --git a/baked.c b/baked.c index b53e8c8..b902c12 100644 --- a/baked.c +++ b/baked.c @@ -1,10 +1,56 @@ -#define INCBIN_PREFIX -#define INCBIN_STYLE INCBIN_STYLE_SNAKE +#include + +#include "gebs/gebs.h" #include "incbin/incbin.h" +#include "stb/stb_ds.h" #include "baked.h" -INCBIN(gpp1, "gpp1"); +INCBIN(gpp1, "./gpp1"); + +INCBIN(home_t, "./tmpls/home.t"); +INCBIN(page_missing_t, "./tmpls/page-missing.t"); + +INCBIN(simple_min_css, "./etc/simple.min.css"); +INCBIN(favicon_ico, "./etc/favicon.ico"); + +Baked_Resource *baked_resources = NULL; + +void add_baked_resource(char *key, const uchar *data, size_t size) +{ + int fd = memfd_create(key, 0); + if (fd < 0) { + LOGE("Could not create resource %s. Aborting...\n", key); + abort(); + } + write(fd, data, size); + shput(baked_resources, key, fd); +} + +void init_baked_resources(void) +{ + add_baked_resource("home.t", home_t_data, home_t_size); + add_baked_resource("page-missing.t", page_missing_t_data, page_missing_t_size); + add_baked_resource("gpp1", gpp1_data, gpp1_size); + add_baked_resource("simple.min.css", simple_min_css_data, simple_min_css_size); + add_baked_resource("favicon.ico", favicon_ico_data, favicon_ico_size); +} + +void free_baked_resources(void) +{ + for (size_t i = 0; i < shlen(baked_resources); i++) { + close(baked_resources[i].value); + } + shfree(baked_resources); +} + +bool get_baked_resource_path(char *key, char *buf, size_t size) +{ + if (shgeti(baked_resources, key) != -1) { + int fd = shget(baked_resources, key); + snprintf(buf, size, "/proc/%d/fd/%d", getpid(), fd); + return true; + } + return false; +} -INCBIN(tmpl_home, "./tmpls/home.t"); -INCBIN(tmpl_page_missing, "./tmpls/page-missing.t"); diff --git a/baked.h b/baked.h index aaa833c..4153c2a 100644 --- a/baked.h +++ b/baked.h @@ -1,13 +1,23 @@ #ifndef BAKED_H_ #define BAKED_H_ -#define INCBIN_PREFIX -#define INCBIN_STYLE INCBIN_STYLE_SNAKE #include "incbin/incbin.h" INCBIN_EXTERN(gpp1); -INCBIN_EXTERN(tmpl_home); -INCBIN_EXTERN(tmpl_page_missing); +INCBIN_EXTERN(home_t); +INCBIN_EXTERN(page_missing_t); + +INCBIN_EXTERN(simple_min_css); +INCBIN_EXTERN(favicon_ico); + +typedef struct { + char *key; // path + int value; // memfd +} Baked_Resource; + +void init_baked_resources(void); +void free_baked_resources(void); +bool get_baked_resource_path(char *key, char *buf, size_t size); #endif // BAKED_H_ diff --git a/build.c b/build.c index 79366f0..b3bcbca 100644 --- a/build.c +++ b/build.c @@ -17,8 +17,6 @@ int main(int argc, char ** argv) "./main.c", "./routes.c", "./baked.c", - "./gpp.c", - "./tmpls.c", "./mongoose.o", "./gpp1" ) { @@ -36,11 +34,13 @@ int main(int argc, char ** argv) } #if DEBUG - CMD("cc", "-ggdb", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba", - "./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o"); + CMD("cc", "-ggdb", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", + "-DINCBIN_PREFIX=", "-DINCBIN_STYLE=INCBIN_STYLE_SNAKE", "-o", "./aboba", + "./main.c", "./routes.c", "./baked.c", "./mongoose.o"); #else - CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba", - "./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o"); + CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", + "-DINCBIN_PREFIX=", "-DINCBIN_STYLE=INCBIN_STYLE_SNAKE", "-o", "./aboba", + "./main.c", "./routes.c", "./baked.c", "./mongoose.o"); #endif } } else if (strcmp(cmd, "clean") == 0) { diff --git a/gpp.c b/gpp.c deleted file mode 100644 index 1c8ccb1..0000000 --- a/gpp.c +++ /dev/null @@ -1,43 +0,0 @@ -#include - -#include "gebs/gebs.h" - -#include "gpp.h" -#include "baked.h" - -int gpp_fd; - -void gpp_init(void) -{ - gpp_fd = memfd_create("gpp-binary", 0); - if (gpp_fd < 0) { - LOGE("Could not create gpp_fd. Aborting..."); - abort(); - } - - write(gpp_fd, gpp1_data, gpp1_size); -} - -void gpp_deinit(void) -{ - close(gpp_fd); -} - -bool gpp_run(char *path, NString_List *env, String_Builder *out) -{ - Cmd cmd = {0}; - defer { cmd_free(&cmd); } - - 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_fd_run_collect(gpp_fd, &cmd, out) == 0; -} - diff --git a/gpp.h b/gpp.h deleted file mode 100644 index 98099d3..0000000 --- a/gpp.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef GPPH_H_ -#define GPPH_H_ - -#include "gebs/gebs.h" - -extern int gpp_fd; - -void gpp_init(void); -void gpp_deinit(void); -bool gpp_run(char *path, NString_List *env, String_Builder *out); - -#endif // GPPH_H_ diff --git a/main.c b/main.c index cd7f843..59523af 100644 --- a/main.c +++ b/main.c @@ -9,8 +9,7 @@ #include "stb/stb_ds.h" #include "routes.h" -#include "gpp.h" -#include "tmpls.h" +#include "baked.h" typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg); @@ -44,8 +43,7 @@ static void init_route_hashtable(void) int main(int argc, char ** argv) { - gpp_init(); - tmpls_init(); + init_baked_resources(); mg_log_set(MG_LL_DEBUG); struct mg_mgr mgr; @@ -62,8 +60,7 @@ int main(int argc, char ** argv) mg_mgr_free(&mgr); shfree(route_hashtable); - tmpls_deinit(); - gpp_deinit(); + free_baked_resources(); return 0; } diff --git a/routes.c b/routes.c index 2950e7a..5934386 100644 --- a/routes.c +++ b/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); } diff --git a/tmpls.c b/tmpls.c deleted file mode 100644 index ec9bacf..0000000 --- a/tmpls.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -#include "gebs/gebs.h" -#include "stb/stb_ds.h" - -#include "tmpls.h" -#include "baked.h" - -Template *tmpls = NULL; - -void create_tmpl(char *name, const unsigned char *data, size_t size) -{ - int fd = memfd_create(name, 0); - if (fd < 0) { - LOGE("Could not create file for template %s. Aborting...\n", name); - abort(); - } - write(fd, data, size); - - shput(tmpls, name, fd); -} - -void tmpls_init(void) -{ - create_tmpl(TMPL_HOME, tmpl_home_data, tmpl_home_size); - create_tmpl(TMPL_PAGE_MISSING, tmpl_page_missing_data, tmpl_page_missing_size); -} - -void tmpls_deinit(void) -{ - for (size_t i = 0; i < shlen(tmpls); i++) { - close(tmpls[i].value); - } - shfree(tmpls); -} - -void tmpl_get_path_by_key(char *key, char *buf, size_t size) -{ - // Check first since we can't have a default value here - if (shgeti(tmpls, key) != -1) { - int fd = shget(tmpls, key); - snprintf(buf, size, "/proc/%d/fd/%d", getpid(), fd); - } -} - diff --git a/tmpls.h b/tmpls.h deleted file mode 100644 index 069f6eb..0000000 --- a/tmpls.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef TMPLS_H_ -#define TMPLS_H_ - -#define TMPL_HOME "home.t" -#define TMPL_PAGE_MISSING "page-missing.t" - -typedef struct { - char *key; - int value; -} Template; - -extern Template *tmpls; - -void tmpls_init(void); -void tmpls_deinit(void); -void tmpl_get_path_by_key(char *key, char *buf, size_t size); - -#endif // TMPLS_H_