Embed templates

This commit is contained in:
kamkow1
2025-06-09 22:10:49 +02:00
parent ac48e49ecc
commit a8c5d7817d
8 changed files with 84 additions and 4 deletions

View File

@ -5,3 +5,6 @@
#include "baked.h"
INCBIN(gpp1, "gpp1");
INCBIN(tmpl_home, "./tmpls/home.t");
INCBIN(tmpl_page_missing, "./tmpls/page-missing.t");

View File

@ -7,4 +7,7 @@
INCBIN_EXTERN(gpp1);
INCBIN_EXTERN(tmpl_home);
INCBIN_EXTERN(tmpl_page_missing);
#endif // BAKED_H_

View File

@ -18,6 +18,7 @@ int main(int argc, char ** argv)
"./routes.c",
"./baked.c",
"./gpp.c",
"./tmpls.c",
"./mongoose.o",
"./gpp1"
) {
@ -36,10 +37,10 @@ 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", "./mongoose.o");
"./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o");
#else
CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba",
"./main.c", "./routes.c", "./baked.c", "./gpp.c", "./mongoose.o");
"./main.c", "./routes.c", "./baked.c", "./gpp.c", "./tmpls.c", "./mongoose.o");
#endif
}
} else if (strcmp(cmd, "clean") == 0) {

3
main.c
View File

@ -10,6 +10,7 @@
#include "routes.h"
#include "gpp.h"
#include "tmpls.h"
typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg);
@ -44,6 +45,7 @@ static void init_route_hashtable(void)
int main(int argc, char ** argv)
{
gpp_init();
tmpls_init();
mg_log_set(MG_LL_DEBUG);
struct mg_mgr mgr;
@ -60,6 +62,7 @@ int main(int argc, char ** argv)
mg_mgr_free(&mgr);
shfree(route_hashtable);
tmpls_deinit();
gpp_deinit();
return 0;

View File

@ -3,6 +3,7 @@
#include "routes.h"
#include "gpp.h"
#include "tmpls.h"
void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg)
{
@ -13,7 +14,11 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
String_Builder out = {0};
defer { sb_free(&out); }
bool ok = gpp_run("./tmpls/page-missing.t", &env, &out);
char path[PATH_MAX] = {0};
tmpl_get_path_by_key(TMPL_PAGE_MISSING, path, sizeof(path));
bool ok = gpp_run(path, &env, &out);
sb_finish(&out);
if (!ok) {
@ -42,7 +47,11 @@ void route_home(struct mg_connection *conn, struct mg_http_message *msg)
String_Builder out = {0};
defer { sb_free(&out); }
bool ok = gpp_run("./tmpls/index.t", &env, &out);
char path[PATH_MAX] = {0};
tmpl_get_path_by_key(TMPL_HOME, path, sizeof(path));
bool ok = gpp_run(path, &env, &out);
sb_finish(&out);
if (!ok) {

43
tmpls.c Normal file
View File

@ -0,0 +1,43 @@
#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)
{
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 Normal file
View File

@ -0,0 +1,18 @@
#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_