124 lines
3.3 KiB
C
124 lines
3.3 KiB
C
#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"
|
|
|
|
typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg);
|
|
|
|
typedef struct {
|
|
char *key; // path
|
|
Route_Handler value;
|
|
} Route;
|
|
|
|
Route *route_hashtable = NULL;
|
|
|
|
void event_handler(struct mg_connection *conn, int ev, void *ev_data)
|
|
{
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
struct mg_http_message *msg = (struct mg_http_message *)ev_data;
|
|
|
|
char key[MG_PATH_MAX] = {0};
|
|
strncpy(key, msg->uri.buf, msg->uri.len);
|
|
Route_Handler handler = shget(route_hashtable, key);
|
|
handler(conn, msg);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
int main(int argc, char ** argv)
|
|
{
|
|
mg_log_set(MG_LL_DEBUG);
|
|
struct mg_mgr mgr;
|
|
mg_mgr_init(&mgr);
|
|
init_route_hashtable();
|
|
|
|
mg_http_listen(&mgr, "http://localhost:8080", &event_handler, NULL);
|
|
|
|
for (;;) {
|
|
mg_mgr_poll(&mgr, 1000);
|
|
scratch_arena_reset();
|
|
}
|
|
|
|
mg_mgr_free(&mgr);
|
|
shfree(route_hashtable);
|
|
|
|
return 0;
|
|
}
|