Embedded gpp
This commit is contained in:
87
main.c
87
main.c
@ -1,11 +1,16 @@
|
||||
#include <libgen.h>
|
||||
#define GEBS_NO_PREFIX
|
||||
|
||||
#define GEBS_IMPLEMENTATION
|
||||
#include "gebs/gebs.h"
|
||||
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
#define STB_DS_IMPLEMENTATION
|
||||
#include "stb/stb_ds.h"
|
||||
|
||||
#include "routes.h"
|
||||
#include "gpp.h"
|
||||
|
||||
typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg);
|
||||
|
||||
typedef struct {
|
||||
@ -27,83 +32,19 @@ void event_handler(struct mg_connection *conn, int ev, void *ev_data)
|
||||
}
|
||||
}
|
||||
|
||||
bool gpp1(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_run_collect(&cmd, out) == 0;
|
||||
}
|
||||
|
||||
void handle_page_not_found(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
{
|
||||
NString_List env = {0};
|
||||
defer { list_free(&env); }
|
||||
|
||||
list_append(&env, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf));
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
bool ok = gpp1("./tmpls/page-missing.t", &env, &out);
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;(");
|
||||
} else {
|
||||
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_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);
|
||||
}
|
||||
|
||||
void handle_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);
|
||||
}
|
||||
|
||||
void handle_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
{
|
||||
NString_List env = {0};
|
||||
defer { list_free(&env); }
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
bool ok = gpp1("./tmpls/index.t", &env, &out);
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;(");
|
||||
} else {
|
||||
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_route_hashtable(void)
|
||||
{
|
||||
shdefault(route_hashtable, &handle_page_not_found);
|
||||
shput(route_hashtable, "/page-missing", &handle_page_not_found);
|
||||
shput(route_hashtable, "/simple.min.css", &handle_simple_css);
|
||||
shput(route_hashtable, "/favicon.ico", &handle_favicon);
|
||||
shput(route_hashtable, "/", &handle_home);
|
||||
shdefault(route_hashtable, &route_page_not_found);
|
||||
shput(route_hashtable, "/page-missing", &route_page_not_found);
|
||||
shput(route_hashtable, "/simple.min.css", &route_simple_css);
|
||||
shput(route_hashtable, "/favicon.ico", &route_favicon);
|
||||
shput(route_hashtable, "/", &route_home);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
gpp_init();
|
||||
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
@ -118,6 +59,8 @@ int main(int argc, char ** argv)
|
||||
|
||||
mg_mgr_free(&mgr);
|
||||
shfree(route_hashtable);
|
||||
|
||||
gpp_deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user