Mercurial
annotate 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 |
| rev | line source |
|---|---|
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "seobeo/seobeo.h" |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
3 volatile sig_atomic_t stop_server = 0; |
|
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
4 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
5 void handle_sigint(int sig) |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
6 { |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
7 printf("Failed\n"); |
|
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
8 stop_server = 1; |
|
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
9 } |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
10 |
| 78 | 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 | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
57 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
58 { |
| 78 | 59 Seobeo_Request_Entry *resp = NULL; |
| 60 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); // 50KB buffer | |
| 61 Seobeo_ServerSideRender(final_body, "/index.html", arena); | |
| 62 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 63 return resp; | |
| 64 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
65 |
| 78 | 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); | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
81 return resp; |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
82 } |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
83 |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
84 |
| 78 | 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 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
93 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
94 Seobeo_Request_Entry* GetUser(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
95 { |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
96 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
97 const char *user_id = id_kv ? ((Seobeo_Request_Entry*)id_kv)->value : "unknown"; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
98 |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
99 Seobeo_Request_Entry *resp = NULL; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
100 char *body = Dowa_Arena_Allocate(arena, 256); |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
101 snprintf(body, 256, "{\"id\":\"%s\",\"name\":\"John Doe\"}", user_id); |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
102 |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
103 Dowa_HashMap_Push_Arena(resp, "body", body, arena); |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
104 return resp; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
105 } |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
106 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
107 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
108 { |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
109 Seobeo_Router_Init(); |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
110 Seobeo_Router_Register("GET", "/", GetHomePage); |
| 78 | 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 | |
|
19
875bb6e10db7
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
16
diff
changeset
|
122 Seobeo_Web_Server_Start("mrjunejune/pages", "6969", SEOBEO_MODE_EDGE, 2); |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
123 } |