CSS and home page

This commit is contained in:
kamkow1
2025-06-09 17:26:25 +02:00
parent bec8cb7102
commit 23de19b63e
7 changed files with 115 additions and 16 deletions

45
main.c
View File

@ -1,3 +1,4 @@
#include <libgen.h>
#define GEBS_NO_PREFIX
#define GEBS_IMPLEMENTATION
#include "gebs/gebs.h"
@ -18,7 +19,10 @@ 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);
char key[MG_PATH_MAX] = {0};
strncpy(key, msg->uri.buf, msg->uri.len);
Route_Handler handler = shget(route_hashtable, key);
handler(conn, msg);
}
}
@ -50,7 +54,36 @@ void handle_page_not_found(struct mg_connection *conn, struct mg_http_message *m
String_Builder out = {0};
defer { sb_free(&out); }
bool ok = gpp1("./tmpls/404.t", &env, &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) {
@ -62,7 +95,11 @@ void handle_page_not_found(struct mg_connection *conn, struct mg_http_message *m
static void init_route_hashtable(void)
{
hmdefault(route_hashtable, &handle_page_not_found);
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)
@ -80,7 +117,7 @@ int main(int argc, char ** argv)
}
mg_mgr_free(&mgr);
hmfree(route_hashtable);
shfree(route_hashtable);
return 0;
}