ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
spa_router.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file spa_router.h
6 * @brief Single-page-app micro-routing + conditional UI streaming (PROTOCORE_ENABLE_SPA_ROUTER).
7 *
8 * A single-page web UI does its own client-side routing: the browser navigates to `/dashboard` or
9 * `/devices/42`, but there is no such file on the device - the server must return the app shell
10 * (`index.html`) and let the JavaScript router take over, while still serving real asset files
11 * (`/app.js`, `/style.css`) and letting API calls (`/api/...`) fall through to their handlers. This is
12 * that routing decision: given a request path, return whether to serve the file, serve the shell, or
13 * pass through to the app.
14 *
15 * The rule: a path under a configured API prefix passes through; a path whose last segment has a file
16 * extension (a dot) is a real asset request; anything else is a client route and gets the shell. Pure,
17 * zero heap, no stdlib, host-testable; the caller wires the result into serve_static / the router.
18 *
19 * ### The fallback HMI
20 *
21 * On a machine-control device the SPA is a convenience, not the contract. If the shell asset is
22 * missing (a half-finished upload, a wiped filesystem), the client will not run scripts, or the
23 * device itself is degraded, an operator still has to be able to see state and actuate something. So
24 * a client route can resolve to PROTOCORE_SPA_SERVE_FALLBACK instead: a plain server-rendered control page
25 * needing no JavaScript and no asset files. The API prefix keeps passing through in that mode - a
26 * fallback page whose endpoints have stopped answering is decoration.
27 *
28 * ### Conditional UI streaming
29 *
30 * That page is assembled from fragments, each with a predicate, and streamed in caller-sized chunks:
31 * only the panels whose condition currently holds are emitted, and a page far larger than any single
32 * buffer never has to fit in RAM. The same streamer serves any conditional UI - showing an operator
33 * only the panels their role, or the machine's current state, warrants.
34 */
35
36#ifndef PROTOCORE_SPA_ROUTER_H
37#define PROTOCORE_SPA_ROUTER_H
38
39#include "protocore_config.h" // the entry point: protocore_types.h for the widths
40
41#if PROTOCORE_ENABLE_SPA_ROUTER
42
43EMBED_BEGIN_DECLS
44
45// This module holds nothing between calls, so it carves no borrow and states none. An entry
46// takes one all the same, and never reads it, so every namespace in the tree is invoked the
47// same way.
48
49/** @brief What to do with a request path. */
50EMBED_ENUM_BYTE(protocore_spa_action,
51 PROTOCORE_SPA_SERVE_FILE, ///< a real asset (has a file extension): serve it statically.
52 PROTOCORE_SPA_SERVE_SHELL, ///< a client route (extensionless): serve the SPA shell (index.html).
53 PROTOCORE_SPA_PASSTHROUGH, ///< under the API prefix: let the app's handlers run.
54 PROTOCORE_SPA_SERVE_FALLBACK, ///< a client route the SPA cannot serve: serve the no-JS control page.
55);
56
57/** @brief What the server currently knows about its ability to serve the SPA. */
58typedef struct
59{
60 const char *api_prefix; ///< paths under this always pass through; null/empty = none.
61 embed_bool shell_available; ///< is the shell asset actually present and servable?
62 embed_bool client_scripting; ///< will the client run the SPA (false = text browser, curl, no-JS)?
63 embed_bool degraded; ///< force the plain control page (recovery mode, failsafe, low memory).
64} protocore_spa_ctx;
65
66/** @brief Predicate deciding whether a fragment is part of this render. */
67typedef embed_bool (*protocore_ui_when_fn)(void *ctx);
68
69/** @brief One UI panel and the condition under which it is shown. Nothing is copied. */
70typedef struct
71{
72 const char *name; ///< label, for diagnostics; not emitted.
73 const char *html; ///< the fragment body (borrowed).
74 protocore_ui_when_fn when; ///< nullptr = always included.
75} protocore_ui_fragment;
76
77/** @brief Cursor over a fragment set. Resumes mid-fragment, so output is chunk-size independent. */
78typedef struct
79{
80 const protocore_ui_fragment *frags;
81 size_t count;
82 void *ctx; ///< passed to each predicate.
83 size_t idx; ///< next fragment to consider.
84 size_t off; ///< bytes of the current fragment already emitted.
85 embed_bool done;
86} protocore_ui_stream;
87
88/** @brief What has_extension takes: path. */
89typedef struct
90{
91 const char *path;
92} SpaRouterHasExtensionArgs;
93
94/** @brief What route takes: path, api_prefix. */
95typedef struct
96{
97 const char *path; ///< the request path (e.g. "/devices/42", "/app.js", "/api/state")
98 const char *api_prefix; ///< a prefix whose paths pass through to handlers (e.g. "/api/"); null/empty = none
99} SpaRouterRouteArgs;
100
101/** @brief What route_ex takes: path, ctx. */
102typedef struct
103{
104 const char *path;
105 const protocore_spa_ctx *ctx;
106} SpaRouterRouteExArgs;
107
108/** @brief What ui_stream_begin takes: s, frags, count, ctx. */
109typedef struct
110{
111 protocore_ui_stream *s;
112 const protocore_ui_fragment *frags;
113 size_t count;
114 void *ctx;
115} SpaRouterUiStreamBeginArgs;
116
117/** @brief What ui_stream_next takes: s, out, cap. */
118typedef struct
119{
120 protocore_ui_stream *s;
121 char *out;
122 size_t cap;
123} SpaRouterUiStreamNextArgs;
124
125/** @brief What ui_stream_done takes: s. */
126typedef struct
127{
128 const protocore_ui_stream *s;
129} SpaRouterUiStreamDoneArgs;
130
131/**
132 * @brief Single-page-app micro-routing + conditional UI streaming (PROTOCORE_ENABLE_SPA_ROUTER). A single-page web UI
133 * ...
134 *
135 * A caller sets the members a call takes, invokes it through ::SpaRouter with the bytes it runs
136 * out of, and reads the outcome off the same handle.
137 *
138 * SpaRouter.has_extension_args.path = ...;
139 * SpaRouter.has_extension(work);
140 * // SpaRouter.ok is what the call reports
141 *
142 * @var SpaRouterNs::has_extension_args what has_extension takes: path
143 * @var SpaRouterNs::route_args what route takes: path, api_prefix
144 * @var SpaRouterNs::route_ex_args what route_ex takes: path, ctx
145 * @var SpaRouterNs::ui_stream_begin_args what ui_stream_begin takes: s, frags, count, ctx
146 * @var SpaRouterNs::ui_stream_next_args what ui_stream_next takes: s, out, cap
147 * @var SpaRouterNs::ui_stream_done_args what ui_stream_done takes: s
148 * @var SpaRouterNs::ok a call's true/false outcome
149 * @var SpaRouterNs::action the routing action. "/" (or empty) serves the shell. A path ...
150 * @var SpaRouterNs::n bytes written; 0 when the stream is finished (or on bad args)
151 * @var SpaRouterNs::has_extension true if the last path segment has a file extension (a '.' after the ...
152 * @var SpaRouterNs::route decide how to route path for a single-page app
153 * @var SpaRouterNs::route_ex decide how to route path, choosing the fallback HMI when the SPA ...
154 * @var SpaRouterNs::ui_stream_begin start streaming frags, evaluating each predicate against ctx. ...
155 * @var SpaRouterNs::ui_stream_next emit up to cap bytes of the remaining included fragments into out. ...
156 * @var SpaRouterNs::ui_stream_done true once every included fragment has been emitted
157 *
158 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
159 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
160 * a caller drives every namespace the same way.
161 */
162typedef struct
163{
164 SpaRouterHasExtensionArgs has_extension_args;
165 SpaRouterRouteArgs route_args;
166 SpaRouterRouteExArgs route_ex_args;
167 SpaRouterUiStreamBeginArgs ui_stream_begin_args;
168 SpaRouterUiStreamNextArgs ui_stream_next_args;
169 SpaRouterUiStreamDoneArgs ui_stream_done_args;
170 embed_bool ok;
171 protocore_spa_action action;
172 size_t n;
173} SpaRouterVars;
174
175/** @brief The operands and the outcome. */
176extern SpaRouterVars SpaRouterV;
177
178/** @brief The entries. */
179typedef struct
180{
181 void (*const has_extension)(const uint8_t *work);
182 void (*const route)(const uint8_t *work);
183 void (*const route_ex)(uint8_t *work);
184 void (*const ui_stream_begin)(const uint8_t *work);
185 void (*const ui_stream_next)(const uint8_t *work);
186 void (*const ui_stream_done)(const uint8_t *work);
187} SpaRouterNs;
188
189// What the table binds, defined once in the .c and taking one parameter each: everything
190// else an entry needs is an operand in SpaRouterV or a region of the borrow at a fixed offset.
191void protocore_spa_router_has_extension(const uint8_t *work);
192void protocore_spa_router_route(const uint8_t *work);
193void protocore_spa_router_route_ex(uint8_t *work);
194void protocore_spa_router_ui_stream_begin(const uint8_t *work);
195void protocore_spa_router_ui_stream_next(const uint8_t *work);
196void protocore_spa_router_ui_stream_done(const uint8_t *work);
197
198// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
199// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
200// `SpaRouter.has_extension(work)` resolves to a named function and becomes a DIRECT call. An extern table
201// leaves the call indirect and the symbol live at every level, -O2 -flto included.
202static const SpaRouterNs SpaRouter __attribute__((unused)) = {
203 .has_extension = protocore_spa_router_has_extension,
204 .route = protocore_spa_router_route,
205 .route_ex = protocore_spa_router_route_ex,
206 .ui_stream_begin = protocore_spa_router_ui_stream_begin,
207 .ui_stream_next = protocore_spa_router_ui_stream_next,
208 .ui_stream_done = protocore_spa_router_ui_stream_done,
209};
210
211EMBED_END_DECLS
212
213#endif // PROTOCORE_ENABLE_SPA_ROUTER
214
215#endif // PROTOCORE_SPA_ROUTER_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.