@@ -3,8 +3,8 @@ import { compile as handlebarsCompile } from 'handlebars';
3
3
import { cmdLine } from './commandLine' ;
4
4
5
5
export type cppCodeSource = {
6
- index : number ;
7
6
filename : string ;
7
+ dataname : string ;
8
8
mime : string ;
9
9
content : Buffer ;
10
10
isGzip : boolean ;
@@ -23,24 +23,17 @@ const psychicTemplate = `
23
23
#include <PsychicHttpsServer.h>
24
24
25
25
{{#each sources}}
26
- {{#if this.isDefault}}
27
- const uint8_t dataDefaultDocument[{{this.length}}] = { {{this.bytes}} };
26
+ const uint8_t data_{{this.dataname}}[{{this.length}}] = { {{this.bytes}} };
28
27
{{#if ../isEtag}}
29
- const char * etagDefaultDocument = "{{this.md5}}";
30
- {{/if}}
31
- {{else}}
32
- const uint8_t data{{this.index}}[{{this.length}}] = { {{this.bytes}} };
33
- {{#if ../isEtag}}
34
- const char * etag{{this.index}} = "{{this.md5}}";
35
- {{/if}}
28
+ const char * etag_{{this.dataname}} = "{{this.md5}}";
36
29
{{/if}}
37
30
{{/each}}
38
31
39
32
void {{methodName}}(PsychicHttpServer * server) {
40
33
{{#each sources}}
41
34
{{#if this.isDefault}}server->defaultEndpoint = {{/if}}server->on("/{{this.filename}}", HTTP_GET, [](PsychicRequest * request) {
42
35
{{#if ../isEtag}}
43
- if (request->hasHeader("If-None-Match") && request->header("If-None-Match") == String({{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if }})) {
36
+ if (request->hasHeader("If-None-Match") && request->header("If-None-Match") == String(etag_{{ this.dataname }})) {
44
37
PsychicResponse response304(request);
45
38
response304.setCode(304);
46
39
return response304.send();
@@ -52,9 +45,9 @@ void {{methodName}}(PsychicHttpServer * server) {
52
45
response.addHeader("Content-Encoding", "gzip");
53
46
{{/if}}
54
47
{{#if ../isEtag}}
55
- response.addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if }});
48
+ response.addHeader("ETag", etag_{{ this.dataname }});
56
49
{{/if}}
57
- response.setContent({{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if}} , sizeof({{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if }}));
50
+ response.setContent(data_{{ this.dataname}} , sizeof(data_{{ this.dataname }}));
58
51
return response.send();
59
52
});
60
53
@@ -72,42 +65,33 @@ const asyncTemplate = `
72
65
#include <ESPAsyncWebServer.h>
73
66
74
67
{{#each sources}}
75
- {{#if this.isDefault}}
76
- const uint8_t dataDefaultDocument[{{this.length}}] = { {{this.bytes}} };
68
+ const uint8_t data_{{this.dataname}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
77
69
{{#if ../isEtag}}
78
- const char * etagDefaultDocument = "{{this.md5}}";
79
- {{/if}}
80
- {{else}}
81
- const uint8_t data{{this.index}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
82
- {{#if ../isEtag}}
83
- const char * etag{{this.index}} = "{{this.md5}}";
84
- {{/if}}
70
+ const char * etag_{{this.dataname}} = "{{this.md5}}";
85
71
{{/if}}
86
72
{{/each}}
87
73
88
74
void {{methodName}}(AsyncWebServer * server) {
89
75
{{#each sources}}
90
- ArRequestHandlerFunction {{#if this.isDefault}}funcDefaultDocument{{else}}func{{this.index}}{{/if }} = [](AsyncWebServerRequest * request) {
76
+ ArRequestHandlerFunction func_{{ this.dataname }} = [](AsyncWebServerRequest * request) {
91
77
{{#if ../isEtag}}
92
- if (request->hasHeader("If-None-Match") && request->getHeader("If-None-Match")->value() == String({{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if }})) {
78
+ if (request->hasHeader("If-None-Match") && request->getHeader("If-None-Match")->value() == String(etag_{{ this.dataname }})) {
93
79
request->send(304);
94
80
return;
95
81
}
96
82
{{/if}}
97
- AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", {{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if }}, {{this.length}});
83
+ AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", data_{{ this.dataname }}, {{this.length}});
98
84
{{#if this.isGzip}}
99
85
response->addHeader("Content-Encoding", "gzip");
100
86
{{/if}}
101
87
{{#if ../isEtag}}
102
- response->addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if }});
88
+ response->addHeader("ETag", etag_{{ this.dataname }});
103
89
{{/if}}
104
90
request->send(response);
105
91
};
92
+ server->on("/{{this.filename}}", HTTP_GET, func_{{this.dataname}});
106
93
{{#if this.isDefault}}
107
- server->on("/{{this.filename}}", HTTP_GET, funcDefaultDocument);
108
- server->on("/", HTTP_GET, funcDefaultDocument);
109
- {{else}}
110
- server->on("/{{this.filename}}", HTTP_GET, func{{this.index}});
94
+ server->on("/", HTTP_GET, func_{{this.dataname}});
111
95
{{/if}}
112
96
113
97
{{/each}}
0 commit comments