comparison design_system/main.c @ 251:117c4d53c9a4

[ui] Add HTML-first Web Component system Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Tue, 04 Aug 2026 09:14:57 -0700
parents
children 7a7581f040e8
comparison
equal deleted inserted replaced
250:745fd127b2a1 251:117c4d53c9a4
1 #include "seobeo/seobeo.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 static Seobeo_Request_Entry *Get_Component_Catalog(
7 Seobeo_Request_Entry *request,
8 Dowa_Arena *arena)
9 {
10 (void)request;
11 size_t file_size = 0;
12 char *source = Seobeo_Web_LoadFile("/index.html", &file_size);
13 if (!source)
14 {
15 Seobeo_Request_Entry *response = NULL;
16 Dowa_HashMap_Push_Arena(response, "status", "500", arena);
17 Dowa_HashMap_Push_Arena(
18 response,
19 "content-type",
20 "text/plain; charset=utf-8",
21 arena);
22 Dowa_HashMap_Push_Arena(
23 response,
24 "body",
25 "Component catalog is unavailable.",
26 arena);
27 return response;
28 }
29
30 char *body = Dowa_Arena_Allocate(arena, file_size + 1);
31 if (!body)
32 {
33 free(source);
34 return NULL;
35 }
36 memcpy(body, source, file_size + 1);
37 free(source);
38
39 char *content_length = Dowa_Arena_Allocate(arena, 32);
40 snprintf(content_length, 32, "%zu", file_size);
41 Seobeo_Request_Entry *response = NULL;
42 Dowa_HashMap_Push_Arena(response, "status", "200", arena);
43 Dowa_HashMap_Push_Arena(
44 response,
45 "content-type",
46 "text/html; charset=utf-8",
47 arena);
48 Dowa_HashMap_Push_Arena(
49 response,
50 "content-length",
51 content_length,
52 arena);
53 Dowa_HashMap_Push_Arena(response, "body", body, arena);
54 return response;
55 }
56
57 static boolean Port_Is_Valid(const char *port)
58 {
59 if (!port || port[0] == '\0')
60 return FALSE;
61 char *end = NULL;
62 long value = strtol(port, &end, 10);
63 return end != port &&
64 *end == '\0' &&
65 value > 0 &&
66 value <= 65535;
67 }
68
69 int main(void)
70 {
71 Seobeo_Router_Init();
72 Seobeo_Router_Register("GET", "/", Get_Component_Catalog);
73 Seobeo_Router_Register("GET", "/tokens", Get_Component_Catalog);
74 Seobeo_Router_Register("GET", "/components", Get_Component_Catalog);
75 Seobeo_Router_Register("GET", "/components/button", Get_Component_Catalog);
76 Seobeo_Router_Register("GET", "/components/card", Get_Component_Catalog);
77 Seobeo_Router_Register("GET", "/components/alert", Get_Component_Catalog);
78 Seobeo_Router_Register("GET", "/components/field", Get_Component_Catalog);
79 Seobeo_Router_Register("GET", "/components/stack", Get_Component_Catalog);
80
81 const char *port = getenv("DESIGN_SYSTEM_PORT");
82 if (!port || port[0] == '\0')
83 port = "6980";
84 if (!Port_Is_Valid(port))
85 {
86 Seobeo_Log(SEOBEO_ERROR, "Invalid design-system port: %s\n", port);
87 Seobeo_Router_Destroy();
88 return 1;
89 }
90 int result = Seobeo_Web_Server_Start_On(
91 "127.0.0.1",
92 "design_system/src",
93 port,
94 SEOBEO_MODE_EDGE,
95 2);
96 Seobeo_Router_Destroy();
97 return result;
98 }