MCPcopy Create free account
hub / github.com/BCsabaEngine/svelteesp32 / genAsyncCpp

Function genAsyncCpp

src/cppCodeAsync.ts:61–117  ·  view source on GitHub ↗
(d: TemplateData)

Source from the content-addressed store, hash-verified

59};
60
61export const genAsyncCpp = (d: TemplateData): string => {
62 const lines: string[] = [
63 `//engine: ESPAsyncWebServer`,
64 `//config: ${d.config}`,
65 ...(d.created ? [`//created: ${d.now}`] : []),
66 '//',
67 genCommonHeader(d),
68 '//',
69 '#include <Arduino.h>',
70 '#include <ESPAsyncWebServer.h>',
71 '//',
72 genDataArrays(d, true),
73 '//',
74 genEtagArrays(d),
75 '//',
76 genManifest(d),
77 '//',
78 genHook(d),
79 '//',
80 '// Http Handlers',
81 `void ${d.methodName}(AsyncWebServer * server) {`
82 ];
83 for (const source of d.sources) {
84 const path = `${d.basePath}/${source.filename}`;
85 const defaultPath = d.basePath || '/';
86 lines.push(
87 '//',
88 `// ${source.filename}`,
89 ` server->on("${path}", HTTP_GET, [](AsyncWebServerRequest * request) {`,
90 genAsyncHandlerBody(d, source, path),
91 ` });`
92 );
93 if (source.isDefault)
94 lines.push(
95 ` server->on("${defaultPath}", HTTP_GET, [](AsyncWebServerRequest * request) {`,
96 genAsyncHandlerBody(d, source, defaultPath),
97 ` });`
98 );
99 }
100 if (d.spa && d.spaSource) {
101 const source = d.spaSource;
102 const path = `${d.basePath}/${source.filename}`;
103 lines.push(
104 '//',
105 `// SPA catch-all: unmatched routes serve ${source.filename}`,
106 ` server->onNotFound([](AsyncWebServerRequest * request) {`,
107 ` if (request->method() != HTTP_GET) { request->send(404); return; }`
108 );
109 if (d.basePath)
110 lines.push(
111 ` if (!request->url().startsWith("${d.basePath}/") && request->url() != "${d.basePath}") { request->send(404); return; }`
112 );
113 lines.push(genAsyncHandlerBody(d, source, path), ` });`);
114 }
115 lines.push('}');
116 return lines.join('\n');
117};

Callers

nothing calls this directly

Calls 6

genCommonHeaderFunction · 0.90
genDataArraysFunction · 0.90
genEtagArraysFunction · 0.90
genManifestFunction · 0.90
genHookFunction · 0.90
genAsyncHandlerBodyFunction · 0.85

Tested by

no test coverage detected