#include #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" #include "tmpls.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); } } static void init_route_hashtable(void) { 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(); tmpls_init(); 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); tmpls_deinit(); gpp_deinit(); return 0; }