comparison mrjunejune/main.c @ 158:1c0878eb17de

[MrJuneJune] Readme file gets compiled in server side.
author June Park <parkjune1995@gmail.com>
date Wed, 14 Jan 2026 07:59:19 -0800
parents 7a63e41a21fb
children f3084bca7317
comparison
equal deleted inserted replaced
157:2db6253f355d 158:1c0878eb17de
1 // Debug mode is now controlled by build target: use //seobeo:seobeo_tcp_server_ws_debug 1 // Debug mode is now controlled by build target: use //seobeo:seobeo_tcp_server_ws_debug
2 #include "seobeo/seobeo.h" 2 #include "seobeo/seobeo.h"
3 #include "markdown_converter/markdown_to_html.h"
3 #include <time.h> 4 #include <time.h>
4 5
5 // UUID + /tmp/ + format (max 4) 6 // UUID + /tmp/ + format (max 4)
6 #define TMP_FILE_LENGTH 47 7 #define TMP_FILE_LENGTH 47
7 #define UUID_LEN 37 8 #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>"
8 28
9 volatile sig_atomic_t stop_server = 0; 29 volatile sig_atomic_t stop_server = 0;
10 static _Atomic uint32_t counter = 0; 30 static _Atomic uint32_t counter = 0;
11 31
12 void handle_sigint(int sig) 32 void handle_sigint(int sig)
13 { 33 {
14 printf("Failed\n"); 34 printf("Failed\n");
15 stop_server = 1; 35 stop_server = 1;
16 } 36 }
17 37
18 void Seobeo_ServerSideRender( 38 void Seobeo_Render_Html(
19 char *final_body, 39 char *final_body,
20 char *path, 40 char *template,
21 Dowa_Arena *arena 41 Dowa_Arena *arena
22 ) { 42 )
23 Seobeo_Log(SEOBEO_DEBUG, "[Curr] %s\n", path); 43 {
24 size_t html_size = 0;
25 char *template = Seobeo_Web_LoadFile(path, &html_size);
26 if (!template) return;
27 Seobeo_Log(SEOBEO_DEBUG, "[Curr] ??\n");
28
29 size_t current_offset = 0; 44 size_t current_offset = 0;
30 char *cursor = template; 45 char *cursor = template;
31 46
32 int32 token_len = 2; 47 int32 token_len = 2;
33 48
59 } 74 }
60 75
61 cursor = end_tag + 2; 76 cursor = end_tag + 2;
62 } 77 }
63 strcpy(final_body + current_offset, cursor); 78 strcpy(final_body + current_offset, cursor);
64 free(template); 79 }
80
81
82 void Seobeo_Render_Html_FilePath(
83 char *final_body,
84 char *path,
85 Dowa_Arena *arena
86 ) {
87 Seobeo_Log(SEOBEO_DEBUG, "[Curr] %s\n", path);
88 size_t html_size = 0;
89 char *template = Seobeo_Web_LoadFile(path, &html_size);
90 if (!template) return;
91 Seobeo_Render_Html(final_body, template, arena);
65 } 92 }
66 93
67 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) 94 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena)
68 { 95 {
69 Seobeo_Request_Entry *resp = NULL; 96 Seobeo_Request_Entry *resp = NULL;
70 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 97 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
71 Seobeo_ServerSideRender(final_body, "/index.html", arena); 98 Seobeo_Render_Html_FilePath(final_body, "/index.html", arena);
72 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 99 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
73 return resp; 100 return resp;
74 } 101 }
75 102
76 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) 103 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena)
77 { 104 {
78 Seobeo_Request_Entry *resp = NULL; 105 Seobeo_Request_Entry *resp = NULL;
79 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 106 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
80 Seobeo_ServerSideRender(final_body, "/resume/index.html", arena); 107 Seobeo_Render_Html_FilePath(final_body, "/resume/index.html", arena);
81 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 108 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
82 return resp; 109 return resp;
83 } 110 }
84 111
85 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) 112 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena)
86 { 113 {
87 Seobeo_Request_Entry *resp = NULL; 114 Seobeo_Request_Entry *resp = NULL;
88 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 115 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
89 Seobeo_ServerSideRender(final_body, "/tools/index.html", arena); 116 Seobeo_Render_Html_FilePath(final_body, "/tools/index.html", arena);
90 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 117 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
91 return resp; 118 return resp;
92 } 119 }
93 120
94 121
95 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) 122 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena)
96 { 123 {
97 Seobeo_Request_Entry *resp = NULL; 124 Seobeo_Request_Entry *resp = NULL;
98 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 125 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
99 Seobeo_ServerSideRender(final_body, "/tools/markdown_to_html/index.html", arena); 126 Seobeo_Render_Html_FilePath(final_body, "/tools/markdown_to_html/index.html", arena);
100 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 127 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
101 return resp; 128 return resp;
102 } 129 }
103 130
104 Seobeo_Request_Entry* GetFileConverter(Seobeo_Request_Entry *req, Dowa_Arena *arena) 131 Seobeo_Request_Entry* GetFileConverter(Seobeo_Request_Entry *req, Dowa_Arena *arena)
105 { 132 {
106 Seobeo_Request_Entry *resp = NULL; 133 Seobeo_Request_Entry *resp = NULL;
107 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 134 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
108 Seobeo_ServerSideRender(final_body, "/tools/file_converter/index.html", arena); 135 Seobeo_Render_Html_FilePath(final_body, "/tools/file_converter/index.html", arena);
109 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 136 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
110 return resp; 137 return resp;
111 } 138 }
112 139
113 Seobeo_Request_Entry *ConvertImageToWebP(Seobeo_Request_Entry *req, Dowa_Arena *arena) 140 Seobeo_Request_Entry *ConvertImageToWebP(Seobeo_Request_Entry *req, Dowa_Arena *arena)
441 468
442 Seobeo_Request_Entry *RenderBlogList(Seobeo_Request_Entry *req, Dowa_Arena *arena) 469 Seobeo_Request_Entry *RenderBlogList(Seobeo_Request_Entry *req, Dowa_Arena *arena)
443 { 470 {
444 Seobeo_Request_Entry *resp = NULL; 471 Seobeo_Request_Entry *resp = NULL;
445 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 472 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
446 Seobeo_ServerSideRender(final_body, "/blog/index.html", arena); 473 Seobeo_Render_Html_FilePath(final_body, "/blog/index.html", arena);
447 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 474 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
448 return resp; 475 return resp;
449 } 476 }
450 477
451 478
452 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena) 479 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena)
453 { 480 {
454 Seobeo_Log(SEOBEO_DEBUG, "[CURR], Hello\n"); 481 Seobeo_Log(SEOBEO_DEBUG, "[CURR], Blog\n");
455 Seobeo_Request_Entry *resp = NULL; 482 Seobeo_Request_Entry *resp = NULL;
456 483
457 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id"); 484 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id");
458 if (!blog_id_kv) 485 if (!blog_id_kv)
459 { 486 {
463 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); 490 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena);
464 return resp; 491 return resp;
465 } 492 }
466 493
467 const char *blog_id = ((Seobeo_Request_Entry *)blog_id_kv)->value; 494 const char *blog_id = ((Seobeo_Request_Entry *)blog_id_kv)->value;
468 char *html_path = Dowa_Arena_Allocate(arena, 512); 495 char *md_path = Dowa_Arena_Allocate(arena, 512);
469 sprintf(html_path, "/blog/%s/index.html", blog_id); 496 sprintf(md_path, "/blog/%s/index.md", blog_id);
470 497
471 char *final_body = Dowa_Arena_Allocate(arena, 100 * 1024); // TODO: Think about the sizes 498 size_t md_file_size = 0;
472 Seobeo_ServerSideRender(final_body, html_path, arena); 499 char *md_file = Seobeo_Web_LoadFile(md_path, &md_file_size);
500
501 char *md = markdown_to_html(md_file);
502 size_t buffer_sizes = 100 * 1024; // TODO: Think about the sizes
503 char *ssr_body = Dowa_Arena_Allocate(arena, buffer_sizes);
504 char *final_body = Dowa_Arena_Allocate(arena, buffer_sizes);
505
506 snprintf(ssr_body, 100*1024, BLOG_HTML, md);
507 Seobeo_Render_Html(final_body, ssr_body, arena);
473 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 508 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
474 return resp; 509 return resp;
475 } 510 }
476 511
477 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) 512 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data)
490 525
491 Seobeo_Request_Entry *GetTalk(Seobeo_Request_Entry *req, Dowa_Arena *arena) 526 Seobeo_Request_Entry *GetTalk(Seobeo_Request_Entry *req, Dowa_Arena *arena)
492 { 527 {
493 Seobeo_Request_Entry *resp = NULL; 528 Seobeo_Request_Entry *resp = NULL;
494 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); 529 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
495 Seobeo_ServerSideRender(final_body, "/talk/index.html", arena); 530 Seobeo_Render_Html_FilePath(final_body, "/talk/index.html", arena);
496 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); 531 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
497 return resp; 532 return resp;
498 } 533 }
499 534
500 CREATE_REDIRECT_HANDLER(HomePage, "/") 535 CREATE_REDIRECT_HANDLER(HomePage, "/")
532 Seobeo_Router_Register("GET", "/blog", RenderBlogList); 567 Seobeo_Router_Register("GET", "/blog", RenderBlogList);
533 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog); 568 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog);
534 569
535 // -- Talk --/ 570 // -- Talk --/
536 Seobeo_Router_Register("GET", "/talk", GetTalk); 571 Seobeo_Router_Register("GET", "/talk", GetTalk);
537 // TODO: Bug where I can't est redriect huh...
538 Seobeo_Router_Register("GET", "/talk/index.html", GetRedirectTalk); 572 Seobeo_Router_Register("GET", "/talk/index.html", GetRedirectTalk);
539
540 printf("Registered Websockets\n");
541 573
542 Seobeo_WebSocket_Server_Init(); 574 Seobeo_WebSocket_Server_Init();
543 Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL); 575 Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL);
544 576
545 Seobeo_Web_Server_Start("mrjunejune/src", "6969", SEOBEO_MODE_EDGE, 3); 577 Seobeo_Web_Server_Start("mrjunejune/src", "6969", SEOBEO_MODE_EDGE, 3);