comparison mrjunejune/main.c @ 169:295ac2e5ec00

[MrJuneJune] Created separate target for generating html from md.
author MrJuneJune <me@mrjunejune.com>
date Mon, 19 Jan 2026 17:33:18 -0800
parents f3084bca7317
children 90dfcef375fb
comparison
equal deleted inserted replaced
168:f3084bca7317 169:295ac2e5ec00
1 // Debug mode is now controlled by build target: use //seobeo:seobeo_tcp_server_ws_debug
2 #include "seobeo/seobeo.h" 1 #include "seobeo/seobeo.h"
3 #include "markdown_converter/markdown_to_html.h" 2 #include "markdown_converter/markdown_to_html.h"
4 #include <time.h> 3 #include <time.h>
5 4
6 // UUID + /tmp/ + format (max 4) 5 // UUID + /tmp/ + format (max 4)
7 #define TMP_FILE_LENGTH 47 6 #define TMP_FILE_LENGTH 47
8 #define UUID_LEN 37 7 #define UUID_LEN 37
9 #define BLOG_HTML "<!DOCTYPE html>" \
10 "<html lang=\"en\">" \
11 "<head>" \
12 " <meta charset=\"UTF-8\">" \
13 " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" \
14 " <title>Multi Threading in JS</title>" \
15 " {{/parts/base_head.html}}" \
16 " <link rel=\"stylesheet\" href=\"/public/highlight/a11y-dark.min.css\" media=\"(prefers-color-scheme: dark)\">" \
17 " <link rel=\"stylesheet\" href=\"/public/highlight/a11y-light.min.css\" media=\"(prefers-color-scheme: light)\">" \
18 " <script src=\"/public/highlight/highlight.min.js\"></script>" \
19 "</head>" \
20 "<body>" \
21 " {{/parts/header.html}}" \
22 " <main>" \
23 "%s" \
24 " </main>" \
25 " {{/parts/footer.html}}" \
26 " <script>hljs.highlightAll();</script>"\
27 "</body>"
28 8
29 volatile sig_atomic_t stop_server = 0; 9 volatile sig_atomic_t stop_server = 0;
30 static _Atomic uint32_t counter = 0; 10 static _Atomic uint32_t counter = 0;
31 11
32 void handle_sigint(int sig) 12 void handle_sigint(int sig)
455 435
456 unlink(filepath); 436 unlink(filepath);
457 437
458 printf("DEBUG: Served and deleted file: %s (%zu bytes)\n", filename, file_size); 438 printf("DEBUG: Served and deleted file: %s (%zu bytes)\n", filename, file_size);
459 439
460 // Set proper Content-Length for binary data
461 char *content_length = Dowa_Arena_Allocate(arena, 32); 440 char *content_length = Dowa_Arena_Allocate(arena, 32);
462 snprintf(content_length, 32, "%zu", file_size); 441 snprintf(content_length, 32, "%zu", file_size);
463 442
464 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); 443 Dowa_HashMap_Push_Arena(resp, "status", "200", arena);
465 Dowa_HashMap_Push_Arena(resp, "content-type", content_type, arena); 444 Dowa_HashMap_Push_Arena(resp, "content-type", content_type, arena);
479 } 458 }
480 459
481 460
482 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena) 461 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena)
483 { 462 {
484 Seobeo_Log(SEOBEO_DEBUG, "[CURR], Blog\n"); 463 Seobeo_Request_Entry *resp = NULL;
485 Seobeo_Request_Entry *resp = NULL; 464
486 465 char *file_path = Dowa_Arena_Allocate(arena, 1024);
487 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id"); 466 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id");
488 if (!blog_id_kv) 467 char *blog_id = ((Seobeo_Request_Entry*)blog_id_kv)->value;
489 { 468 snprintf(file_path, 1024, "/blog/%s/index.html", blog_id);
490 char *error_msg = "No Blog Id"; 469
491 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); 470 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
492 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); 471 Seobeo_Render_Html_FilePath(final_body, file_path, arena);
493 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena);
494 return resp;
495 }
496
497 const char *blog_id = ((Seobeo_Request_Entry *)blog_id_kv)->value;
498 char *md_path = Dowa_Arena_Allocate(arena, 512);
499 sprintf(md_path, "/blog/%s/index.md", blog_id);
500
501 size_t md_file_size = 0;
502 char *md_file = Seobeo_Web_LoadFile(md_path, &md_file_size);
503
504 char *md = markdown_to_html(md_file);
505 size_t buffer_sizes = 100 * 1024; // TODO: Think about the sizes
506 char *ssr_body = Dowa_Arena_Allocate(arena, buffer_sizes);
507 char *final_body = Dowa_Arena_Allocate(arena, buffer_sizes);
508
509 snprintf(ssr_body, 100*1024, BLOG_HTML, md);
510 Seobeo_Render_Html(final_body, ssr_body, arena);
511 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 472 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
512 return resp; 473 return resp;
513 } 474 }
514 475
515 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) 476 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data)