Simple server
This commit is contained in:
86
main.c
Normal file
86
main.c
Normal file
@ -0,0 +1,86 @@
|
||||
#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;
|
||||
Route_Handler handler = hmget(route_hashtable, msg->uri.buf);
|
||||
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/404.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)
|
||||
{
|
||||
hmdefault(route_hashtable, &handle_page_not_found);
|
||||
}
|
||||
|
||||
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);
|
||||
hmfree(route_hashtable);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user