Mercurial
diff mrjunejune/main.c @ 173:827c6ac504cd hg-web
Merged in default here.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 19 Jan 2026 18:59:10 -0800 |
| parents | 295ac2e5ec00 |
| children | 90dfcef375fb |
line wrap: on
line diff
--- a/mrjunejune/main.c Sat Jan 10 13:35:09 2026 -0800 +++ b/mrjunejune/main.c Mon Jan 19 18:59:10 2026 -0800 @@ -1,5 +1,5 @@ -// Debug mode is now controlled by build target: use //seobeo:seobeo_tcp_server_ws_debug #include "seobeo/seobeo.h" +#include "markdown_converter/markdown_to_html.h" #include <time.h> // UUID + /tmp/ + format (max 4) @@ -15,17 +15,12 @@ stop_server = 1; } -void Seobeo_ServerSideRender( +void Seobeo_Render_Html( char *final_body, - char *path, + char *template, Dowa_Arena *arena -) { - Seobeo_Log(SEOBEO_DEBUG, "[Curr] %s\n", path); - size_t html_size = 0; - char *template = Seobeo_Web_LoadFile(path, &html_size); - if (!template) return; - Seobeo_Log(SEOBEO_DEBUG, "[Curr] ??\n"); - +) +{ size_t current_offset = 0; char *cursor = template; @@ -61,14 +56,26 @@ cursor = end_tag + 2; } strcpy(final_body + current_offset, cursor); - free(template); +} + + +void Seobeo_Render_Html_FilePath( + char *final_body, + char *path, + Dowa_Arena *arena +) { + Seobeo_Log(SEOBEO_DEBUG, "[Curr] %s\n", path); + size_t html_size = 0; + char *template = Seobeo_Web_LoadFile(path, &html_size); + if (!template) return; + Seobeo_Render_Html(final_body, template, arena); } 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); + Seobeo_Render_Html_FilePath(final_body, "/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -77,7 +84,7 @@ { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); - Seobeo_ServerSideRender(final_body, "/resume/index.html", arena); + Seobeo_Render_Html_FilePath(final_body, "/resume/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -86,7 +93,7 @@ { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); - Seobeo_ServerSideRender(final_body, "/tools/index.html", arena); + Seobeo_Render_Html_FilePath(final_body, "/tools/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -96,7 +103,7 @@ { 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); + Seobeo_Render_Html_FilePath(final_body, "/tools/markdown_to_html/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -105,7 +112,7 @@ { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); - Seobeo_ServerSideRender(final_body, "/tools/file_converter/index.html", arena); + Seobeo_Render_Html_FilePath(final_body, "/tools/file_converter/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -160,7 +167,6 @@ printf("DEBUG: Converting image, file_size=%zu bytes\n", file_size); - int open_flags = O_RDWR | O_CREAT | O_EXCL; char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); @@ -279,13 +285,19 @@ printf("DEBUG: Converting video, file_size=%zu bytes\n", file_size); + int open_flags = O_RDWR | O_CREAT | O_EXCL; + char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); - Dowa_String_UUID((uint32)time(NULL), uuid4); + uint32 seed = (uint32)time(NULL) ^ (uint32)pthread_self() ^ counter++; + Dowa_String_UUID(seed, uuid4); char *input_path = Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH); snprintf(input_path, TMP_FILE_LENGTH, "/tmp/%s", uuid4); - int input_fd = mkstemp(input_path); + Seobeo_Log(SEOBEO_DEBUG, "Input path: %s\n", input_path); + + int input_fd = open(input_path, open_flags, 0600); if (input_fd == -1) { + Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); char *error_msg = "Failed to create temporary file"; Dowa_HashMap_Push_Arena(resp, "status", "500", arena); Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); @@ -296,9 +308,7 @@ write(input_fd, file_data, file_size); close(input_fd); - int open_flags = O_RDWR | O_CREAT | O_EXCL; - - uint32 seed = (uint32)time(NULL) ^ (uint32)pthread_self() ^ counter++;; + seed = (uint32)time(NULL) ^ (uint32)pthread_self() ^ counter++; Dowa_String_UUID(seed, uuid4); char *output_path = (char *)Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH);; snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.mp4", uuid4); @@ -427,7 +437,6 @@ printf("DEBUG: Served and deleted file: %s (%zu bytes)\n", filename, file_size); - // Set proper Content-Length for binary data char *content_length = Dowa_Arena_Allocate(arena, 32); snprintf(content_length, 32, "%zu", file_size); @@ -443,7 +452,7 @@ { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); - Seobeo_ServerSideRender(final_body, "/blog/index.html", arena); + Seobeo_Render_Html_FilePath(final_body, "/blog/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -451,25 +460,15 @@ Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena) { - Seobeo_Log(SEOBEO_DEBUG, "[CURR], Hello\n"); Seobeo_Request_Entry *resp = NULL; + char *file_path = Dowa_Arena_Allocate(arena, 1024); void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id"); - if (!blog_id_kv) - { - char *error_msg = "No Blog Id"; - Dowa_HashMap_Push_Arena(resp, "status", "404", arena); - Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); - Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); - return resp; - } + char *blog_id = ((Seobeo_Request_Entry*)blog_id_kv)->value; + snprintf(file_path, 1024, "/blog/%s/index.html", blog_id); - const char *blog_id = ((Seobeo_Request_Entry *)blog_id_kv)->value; - char *html_path = Dowa_Arena_Allocate(arena, 512); - sprintf(html_path, "/blog/%s/index.html", blog_id); - - char *final_body = Dowa_Arena_Allocate(arena, 100 * 1024); // TODO: Think about the sizes - Seobeo_ServerSideRender(final_body, html_path, arena); + char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); + Seobeo_Render_Html_FilePath(final_body, file_path, arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -492,7 +491,7 @@ { Seobeo_Request_Entry *resp = NULL; char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); - Seobeo_ServerSideRender(final_body, "/talk/index.html", arena); + Seobeo_Render_Html_FilePath(final_body, "/talk/index.html", arena); Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); return resp; } @@ -534,11 +533,8 @@ // -- Talk --/ Seobeo_Router_Register("GET", "/talk", GetTalk); - // TODO: Bug where I can't est redriect huh... Seobeo_Router_Register("GET", "/talk/index.html", GetRedirectTalk); - printf("Registered Websockets\n"); - Seobeo_WebSocket_Server_Init(); Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL);