Keep all baked assets in one place
This commit is contained in:
56
baked.c
56
baked.c
@ -1,10 +1,56 @@
|
|||||||
#define INCBIN_PREFIX
|
#include <sys/mman.h>
|
||||||
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
|
|
||||||
|
#include "gebs/gebs.h"
|
||||||
#include "incbin/incbin.h"
|
#include "incbin/incbin.h"
|
||||||
|
#include "stb/stb_ds.h"
|
||||||
|
|
||||||
#include "baked.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");
|
|
||||||
|
18
baked.h
18
baked.h
@ -1,13 +1,23 @@
|
|||||||
#ifndef BAKED_H_
|
#ifndef BAKED_H_
|
||||||
#define BAKED_H_
|
#define BAKED_H_
|
||||||
|
|
||||||
#define INCBIN_PREFIX
|
|
||||||
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
|
|
||||||
#include "incbin/incbin.h"
|
#include "incbin/incbin.h"
|
||||||
|
|
||||||
INCBIN_EXTERN(gpp1);
|
INCBIN_EXTERN(gpp1);
|
||||||
|
|
||||||
INCBIN_EXTERN(tmpl_home);
|
INCBIN_EXTERN(home_t);
|
||||||
INCBIN_EXTERN(tmpl_page_missing);
|
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_
|
#endif // BAKED_H_
|
||||||
|
12
build.c
12
build.c
@ -17,8 +17,6 @@ int main(int argc, char ** argv)
|
|||||||
"./main.c",
|
"./main.c",
|
||||||
"./routes.c",
|
"./routes.c",
|
||||||
"./baked.c",
|
"./baked.c",
|
||||||
"./gpp.c",
|
|
||||||
"./tmpls.c",
|
|
||||||
"./mongoose.o",
|
"./mongoose.o",
|
||||||
"./gpp1"
|
"./gpp1"
|
||||||
) {
|
) {
|
||||||
@ -36,11 +34,13 @@ int main(int argc, char ** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
CMD("cc", "-ggdb", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba",
|
CMD("cc", "-ggdb", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX",
|
||||||
"./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o");
|
"-DINCBIN_PREFIX=", "-DINCBIN_STYLE=INCBIN_STYLE_SNAKE", "-o", "./aboba",
|
||||||
|
"./main.c", "./routes.c", "./baked.c", "./mongoose.o");
|
||||||
#else
|
#else
|
||||||
CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba",
|
CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX",
|
||||||
"./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o");
|
"-DINCBIN_PREFIX=", "-DINCBIN_STYLE=INCBIN_STYLE_SNAKE", "-o", "./aboba",
|
||||||
|
"./main.c", "./routes.c", "./baked.c", "./mongoose.o");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} else if (strcmp(cmd, "clean") == 0) {
|
} else if (strcmp(cmd, "clean") == 0) {
|
||||||
|
43
gpp.c
43
gpp.c
@ -1,43 +0,0 @@
|
|||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
12
gpp.h
12
gpp.h
@ -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_
|
|
9
main.c
9
main.c
@ -9,8 +9,7 @@
|
|||||||
#include "stb/stb_ds.h"
|
#include "stb/stb_ds.h"
|
||||||
|
|
||||||
#include "routes.h"
|
#include "routes.h"
|
||||||
#include "gpp.h"
|
#include "baked.h"
|
||||||
#include "tmpls.h"
|
|
||||||
|
|
||||||
typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg);
|
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)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
gpp_init();
|
init_baked_resources();
|
||||||
tmpls_init();
|
|
||||||
|
|
||||||
mg_log_set(MG_LL_DEBUG);
|
mg_log_set(MG_LL_DEBUG);
|
||||||
struct mg_mgr mgr;
|
struct mg_mgr mgr;
|
||||||
@ -62,8 +60,7 @@ int main(int argc, char ** argv)
|
|||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
shfree(route_hashtable);
|
shfree(route_hashtable);
|
||||||
|
|
||||||
tmpls_deinit();
|
free_baked_resources();
|
||||||
gpp_deinit();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
76
routes.c
76
routes.c
@ -2,8 +2,32 @@
|
|||||||
#include "mongoose/mongoose.h"
|
#include "mongoose/mongoose.h"
|
||||||
|
|
||||||
#include "routes.h"
|
#include "routes.h"
|
||||||
#include "gpp.h"
|
#include "baked.h"
|
||||||
#include "tmpls.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)
|
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); }
|
defer { sb_free(&out); }
|
||||||
|
|
||||||
char path[PATH_MAX] = {0};
|
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);
|
bool ok = gpp_run(path, &env, &out);
|
||||||
sb_finish(&out);
|
sb_finish(&out);
|
||||||
|
|
||||||
if (!ok) {
|
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 {
|
} else {
|
||||||
mg_http_reply(conn, 404, "Content-Type: text/html\r\n", out.items);
|
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)
|
void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg)
|
||||||
{
|
{
|
||||||
struct mg_http_serve_opts opts = { 0 };
|
char path[PATH_MAX] = {0};
|
||||||
mg_http_serve_file(conn, msg, "./assets/simple.min.css", &opts);
|
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)
|
void route_favicon(struct mg_connection *conn, struct mg_http_message *msg)
|
||||||
{
|
{
|
||||||
struct mg_http_serve_opts opts = { 0 };
|
char path[PATH_MAX] = {0};
|
||||||
mg_http_serve_file(conn, msg, "./assets/favicon.ico", &opts);
|
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)
|
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); }
|
defer { sb_free(&out); }
|
||||||
|
|
||||||
char path[PATH_MAX] = {0};
|
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);
|
bool ok = gpp_run(path, &env, &out);
|
||||||
sb_finish(&out);
|
sb_finish(&out);
|
||||||
|
|
||||||
if (!ok) {
|
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 {
|
} else {
|
||||||
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
||||||
}
|
}
|
||||||
|
46
tmpls.c
46
tmpls.c
@ -1,46 +0,0 @@
|
|||||||
#include <sys/mman.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
18
tmpls.h
18
tmpls.h
@ -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_
|
|
Reference in New Issue
Block a user