Simple server

This commit is contained in:
kamkow1
2025-06-09 14:42:51 +02:00
commit bec8cb7102
9 changed files with 5302 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
mongoose.o
aboba
build
gpp1

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "mongoose"]
path = mongoose
url = https://github.com/cesanta/mongoose.git
[submodule "gebs"]
path = gebs
url = https://gitlab.com/kamkow1/gebs.git

32
build.c Normal file
View File

@ -0,0 +1,32 @@
#define GEBS_NO_PREFIX
#define GEBS_IMPLEMENTATION
#include "gebs/gebs.h"
char *prog = NULL;
int main(int argc, char ** argv)
{
rebuild_self(argc, argv, "cc", "-o", "build", "build.c");
prog = SHIFT(&argc, &argv);
char *cmd = SHIFT(&argc, &argv);
if (strcmp(cmd, "make") == 0) {
RULE("./aboba", "./main.c", "./mongoose.o", "./gpp1") {
RULE("./mongoose.o", "./mongoose/mongoose.c") {
CMD("cc", "-c", "-o", "./mongoose.o", "./mongoose/mongoose.c");
}
RULE("./gpp1", "./gpp/gpp.c") {
CMD("cc", "-DHAVE_STRDUP", "-DHAVE_FNMATCH_H", "-o", "gpp1", "gpp/gpp.c");
}
CMD("cc", "-o", "./aboba", "./main.c", "./mongoose.o");
}
} else if (strcmp(cmd, "clean") == 0) {
remove1("./build");
remove1("./aboba");
remove("./mongoose.o");
}
return 0;
}

1
gebs Submodule

Submodule gebs added at 9582176638

3267
gpp/gpp.c Normal file

File diff suppressed because it is too large Load Diff

86
main.c Normal file
View 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;
}

1
mongoose Submodule

Submodule mongoose added at 9411f1c7b6

1895
stb/stb_ds.h Normal file

File diff suppressed because it is too large Load Diff

10
tmpls/404.t Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>404 - Page not found</title>
</head>
<body>
The page you were looking for doesn't exist. <br />
URL: <#URL>
</body>
</html>