Mercurial
comparison mrjunejune/main.c @ 78:e7bf9e002850
amend
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 31 Dec 2025 15:07:43 -0800 |
| parents | c348ac875294 |
| children | 5710108c949e |
comparison
equal
deleted
inserted
replaced
| 77:c348ac875294 | 78:e7bf9e002850 |
|---|---|
| 6 { | 6 { |
| 7 printf("Failed\n"); | 7 printf("Failed\n"); |
| 8 stop_server = 1; | 8 stop_server = 1; |
| 9 } | 9 } |
| 10 | 10 |
| 11 void Seobeo_ServerSideRender( | |
| 12 char *final_body, | |
| 13 char *path, | |
| 14 Dowa_Arena *arena | |
| 15 ) { | |
| 16 size_t html_size = 0; | |
| 17 char *template = Seobeo_Web_LoadFile(path, &html_size); | |
| 18 if (!template) return; | |
| 19 | |
| 20 size_t current_offset = 0; | |
| 21 char *cursor = template; | |
| 22 | |
| 23 int32 token_len = 2; | |
| 24 | |
| 25 while (true) | |
| 26 { | |
| 27 char *start_tag = strstr(cursor, "{{"); | |
| 28 if (!start_tag) break; | |
| 29 | |
| 30 char *end_tag = strstr(start_tag, "}}"); | |
| 31 if (!end_tag) break; | |
| 32 | |
| 33 size_t leading_len = start_tag - cursor; | |
| 34 memcpy(final_body + current_offset, cursor, leading_len); | |
| 35 current_offset += leading_len; | |
| 36 | |
| 37 size_t name_len = end_tag - (start_tag + token_len); | |
| 38 char *include_name = Dowa_Arena_Allocate(arena, name_len + 1); | |
| 39 memcpy(include_name, start_tag + token_len, name_len); | |
| 40 include_name[name_len] = '\0'; | |
| 41 | |
| 42 size_t sub_file_size = 0; | |
| 43 char *sub_content = Seobeo_Web_LoadFile(include_name, &sub_file_size); | |
| 44 if (sub_content) | |
| 45 { | |
| 46 memcpy(final_body + current_offset, sub_content, sub_file_size); | |
| 47 current_offset += sub_file_size; | |
| 48 free(sub_content); | |
| 49 } | |
| 50 | |
| 51 cursor = end_tag + 2; | |
| 52 } | |
| 53 strcpy(final_body + current_offset, cursor); | |
| 54 free(template); | |
| 55 } | |
| 56 | |
| 11 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) | 57 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 12 { | 58 { |
| 13 Seobeo_Request_Entry *resp = NULL; | 59 Seobeo_Request_Entry *resp = NULL; |
| 14 char *body = Dowa_Arena_Allocate(arena, 10 * 1024); | 60 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); // 50KB buffer |
| 15 size_t body_size = 0; | 61 Seobeo_ServerSideRender(final_body, "/index.html", arena); |
| 16 char *file_content = Seobeo_Web_LoadFile("/index.html", &body_size); | 62 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 17 snprintf(body, 10 * 1024, file_content); | 63 return resp; |
| 18 free(file_content); | 64 } |
| 19 | 65 |
| 20 Dowa_HashMap_Push_Arena(resp, "body", body, arena); | 66 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 67 { | |
| 68 Seobeo_Request_Entry *resp = NULL; | |
| 69 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 70 Seobeo_ServerSideRender(final_body, "/resume/index.html", arena); | |
| 71 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 72 return resp; | |
| 73 } | |
| 74 | |
| 75 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 76 { | |
| 77 Seobeo_Request_Entry *resp = NULL; | |
| 78 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 79 Seobeo_ServerSideRender(final_body, "/tools/index.html", arena); | |
| 80 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 21 return resp; | 81 return resp; |
| 22 } | 82 } |
| 23 | 83 |
| 24 | 84 |
| 85 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 86 { | |
| 87 Seobeo_Request_Entry *resp = NULL; | |
| 88 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 89 Seobeo_ServerSideRender(final_body, "/tools/markdown_to_html/index.html", arena); | |
| 90 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 91 return resp; | |
| 92 } | |
| 25 | 93 |
| 26 Seobeo_Request_Entry* GetUser(Seobeo_Request_Entry *req, Dowa_Arena *arena) | 94 Seobeo_Request_Entry* GetUser(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 27 { | 95 { |
| 28 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | 96 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); |
| 29 const char *user_id = id_kv ? ((Seobeo_Request_Entry*)id_kv)->value : "unknown"; | 97 const char *user_id = id_kv ? ((Seobeo_Request_Entry*)id_kv)->value : "unknown"; |
| 34 | 102 |
| 35 Dowa_HashMap_Push_Arena(resp, "body", body, arena); | 103 Dowa_HashMap_Push_Arena(resp, "body", body, arena); |
| 36 return resp; | 104 return resp; |
| 37 } | 105 } |
| 38 | 106 |
| 39 | |
| 40 int main(void) | 107 int main(void) |
| 41 { | 108 { |
| 42 Seobeo_Router_Init(); | 109 Seobeo_Router_Init(); |
| 43 Seobeo_Router_Register("GET", "/", GetHomePage); | 110 Seobeo_Router_Register("GET", "/", GetHomePage); |
| 111 Seobeo_Router_Register("GET", "/index.html", GetHomePage); | |
| 112 | |
| 113 Seobeo_Router_Register("GET", "/resume", GetResume); | |
| 114 Seobeo_Router_Register("GET", "/resume/index.html", GetResume); | |
| 115 | |
| 116 Seobeo_Router_Register("GET", "/tools", GetTools); | |
| 117 Seobeo_Router_Register("GET", "/tools/index.html", GetTools); | |
| 118 | |
| 119 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
| 120 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetMDToHTML); | |
| 121 | |
| 44 Seobeo_Web_Server_Start("mrjunejune/pages", "6969", SEOBEO_MODE_EDGE, 2); | 122 Seobeo_Web_Server_Start("mrjunejune/pages", "6969", SEOBEO_MODE_EDGE, 2); |
| 45 } | 123 } |