Mercurial
annotate mrjunejune/main.c @ 86:431df06b1a9b
Fixed few things.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 01 Jan 2026 14:05:34 -0800 |
| parents | bcc76a156aea |
| children | a3962e681490 |
| 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); | |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
79
diff
changeset
|
44 printf("sub_content %s", sub_content); |
| 78 | 45 if (sub_content) |
| 46 { | |
| 47 memcpy(final_body + current_offset, sub_content, sub_file_size); | |
| 48 current_offset += sub_file_size; | |
| 49 free(sub_content); | |
| 50 } | |
| 51 | |
| 52 cursor = end_tag + 2; | |
| 53 } | |
| 54 strcpy(final_body + current_offset, cursor); | |
| 55 free(template); | |
| 56 } | |
| 57 | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
58 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
|
59 { |
| 78 | 60 Seobeo_Request_Entry *resp = NULL; |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
79
diff
changeset
|
61 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
| 78 | 62 Seobeo_ServerSideRender(final_body, "/index.html", arena); |
| 63 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 64 return resp; | |
| 65 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
66 |
| 78 | 67 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 68 { | |
| 69 Seobeo_Request_Entry *resp = NULL; | |
| 70 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 71 Seobeo_ServerSideRender(final_body, "/resume/index.html", arena); | |
| 72 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 73 return resp; | |
| 74 } | |
| 75 | |
| 76 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 77 { | |
| 78 Seobeo_Request_Entry *resp = NULL; | |
| 79 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 80 Seobeo_ServerSideRender(final_body, "/tools/index.html", arena); | |
| 81 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
82 return resp; |
|
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 |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
85 |
| 78 | 86 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 87 { | |
| 88 Seobeo_Request_Entry *resp = NULL; | |
| 89 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 90 Seobeo_ServerSideRender(final_body, "/tools/markdown_to_html/index.html", arena); | |
| 91 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 92 return resp; | |
| 93 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
94 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
95 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
|
96 { |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
97 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
|
98 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
|
99 |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
104 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
|
105 return resp; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
106 } |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
107 |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
108 CREATE_REDIRECT_HANDLER(HomePage, "http://localhost:6969/") |
|
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
109 CREATE_REDIRECT_HANDLER(Resume, "http://localhost:6969/resume") |
|
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
110 CREATE_REDIRECT_HANDLER(Tools, "http://localhost:6969/tools") |
|
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
111 CREATE_REDIRECT_HANDLER(MarkDownToHtml, "http://localhost:6969/tools/markdown_to_html") |
|
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
112 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
113 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
114 { |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
115 Seobeo_Router_Init(); |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
116 Seobeo_Router_Register("GET", "/", GetHomePage); |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
117 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); |
| 78 | 118 |
| 119 Seobeo_Router_Register("GET", "/resume", GetResume); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
120 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); |
| 78 | 121 |
| 122 Seobeo_Router_Register("GET", "/tools", GetTools); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
123 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); |
| 78 | 124 |
| 125 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
126 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); |
| 78 | 127 |
|
84
bcc76a156aea
Updated to be called src instead of pages.
June Park <parkjune1995@gmail.com>
parents:
80
diff
changeset
|
128 Seobeo_Web_Server_Start("mrjunejune/src", "6969", SEOBEO_MODE_EDGE, 2); |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
129 } |