Mercurial
view mrjunejune/main.c @ 86:431df06b1a9b
Fixed few things.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 01 Jan 2026 14:05:34 -0800 |
| parents | bcc76a156aea |
| children | a3962e681490 |
line wrap: on
line source
#include "seobeo/seobeo.h" volatile sig_atomic_t stop_server = 0; void handle_sigint(int sig) { printf("Failed\n"); stop_server = 1; } void Seobeo_ServerSideRender( char *final_body, char *path, Dowa_Arena *arena ) { size_t html_size = 0; char *template = Seobeo_Web_LoadFile(path, &html_size); if (!template) return; size_t current_offset = 0; char *cursor = template; int32 token_len = 2; while (true) { char *start_tag = strstr(cursor, "{{"); if (!start_tag) break; char *end_tag = strstr(start_tag, "}}"); if (!end_tag) break; size_t leading_len = start_tag - cursor; memcpy(final_body + current_offset, cursor, leading_len); current_offset += leading_len; size_t name_len = end_tag - (start_tag + token_len); char *include_name = Dowa_Arena_Allocate(arena, name_len + 1); memcpy(include_name, start_tag + token_len, name_len); include_name[name_len] = '\0'; size_t sub_file_size = 0; char *sub_content = Seobeo_Web_LoadFile(include_name, &sub_file_size); printf("sub_content %s", sub_content); if (sub_content) { memcpy(final_body + current_offset, sub_content, sub_file_size); current_offset += sub_file_size; free(sub_content); } cursor = end_tag + 2; } strcpy(final_body + current_offset, cursor); free(template); } Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); Seobeo_ServerSideRender(final_body, "/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); Seobeo_ServerSideRender(final_body, "/resume/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); Seobeo_ServerSideRender(final_body, "/tools/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); Seobeo_ServerSideRender(final_body, "/tools/markdown_to_html/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } Seobeo_Request_Entry* GetUser(Seobeo_Request_Entry *req, Dowa_Arena *arena) { void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); const char *user_id = id_kv ? ((Seobeo_Request_Entry*)id_kv)->value : "unknown"; Seobeo_Request_Entry *resp = NULL; char *body = Dowa_Arena_Allocate(arena, 256); snprintf(body, 256, "{\"id\":\"%s\",\"name\":\"John Doe\"}", user_id); Dowa_HashMap_Push_Arena(resp, "body", body, arena); return resp; } CREATE_REDIRECT_HANDLER(HomePage, "http://localhost:6969/") CREATE_REDIRECT_HANDLER(Resume, "http://localhost:6969/resume") CREATE_REDIRECT_HANDLER(Tools, "http://localhost:6969/tools") CREATE_REDIRECT_HANDLER(MarkDownToHtml, "http://localhost:6969/tools/markdown_to_html") int main(void) { Seobeo_Router_Init(); Seobeo_Router_Register("GET", "/", GetHomePage); Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); Seobeo_Router_Register("GET", "/resume", GetResume); Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); Seobeo_Router_Register("GET", "/tools", GetTools); Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); Seobeo_Web_Server_Start("mrjunejune/src", "6969", SEOBEO_MODE_EDGE, 2); }