Skip to content

Commit 655adb5

Browse files
committed
feat: named document instead of index
1 parent a8ffa04 commit 655adb5

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

src/cppCode.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { compile as handlebarsCompile } from 'handlebars';
33
import { cmdLine } from './commandLine';
44

55
export type cppCodeSource = {
6-
index: number;
76
filename: string;
7+
dataname: string;
88
mime: string;
99
content: Buffer;
1010
isGzip: boolean;
@@ -23,24 +23,17 @@ const psychicTemplate = `
2323
#include <PsychicHttpsServer.h>
2424
2525
{{#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}} };
2827
{{#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}}";
3629
{{/if}}
3730
{{/each}}
3831
3932
void {{methodName}}(PsychicHttpServer * server) {
4033
{{#each sources}}
4134
{{#if this.isDefault}}server->defaultEndpoint = {{/if}}server->on("/{{this.filename}}", HTTP_GET, [](PsychicRequest * request) {
4235
{{#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}})) {
4437
PsychicResponse response304(request);
4538
response304.setCode(304);
4639
return response304.send();
@@ -52,9 +45,9 @@ void {{methodName}}(PsychicHttpServer * server) {
5245
response.addHeader("Content-Encoding", "gzip");
5346
{{/if}}
5447
{{#if ../isEtag}}
55-
response.addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}});
48+
response.addHeader("ETag", etag_{{this.dataname}});
5649
{{/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}}));
5851
return response.send();
5952
});
6053
@@ -72,42 +65,33 @@ const asyncTemplate = `
7265
#include <ESPAsyncWebServer.h>
7366
7467
{{#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}} };
7769
{{#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}}";
8571
{{/if}}
8672
{{/each}}
8773
8874
void {{methodName}}(AsyncWebServer * server) {
8975
{{#each sources}}
90-
ArRequestHandlerFunction {{#if this.isDefault}}funcDefaultDocument{{else}}func{{this.index}}{{/if}} = [](AsyncWebServerRequest * request) {
76+
ArRequestHandlerFunction func_{{this.dataname}} = [](AsyncWebServerRequest * request) {
9177
{{#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}})) {
9379
request->send(304);
9480
return;
9581
}
9682
{{/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}});
9884
{{#if this.isGzip}}
9985
response->addHeader("Content-Encoding", "gzip");
10086
{{/if}}
10187
{{#if ../isEtag}}
102-
response->addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}});
88+
response->addHeader("ETag", etag_{{this.dataname}});
10389
{{/if}}
10490
request->send(response);
10591
};
92+
server->on("/{{this.filename}}", HTTP_GET, func_{{this.dataname}});
10693
{{#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}});
11195
{{/if}}
11296
11397
{{/each}}

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const summary = {
1717
};
1818

1919
const sources: cppCodeSource[] = [];
20-
let sourceIndex = 0;
2120
const files = getFiles();
2221
if (files.length === 0) {
2322
console.error(`Directory ${cmdLine.sourcepath} is empty`);
@@ -29,13 +28,16 @@ for (const file of files) {
2928
summary.filecount++;
3029
console.log(`[${file}]`);
3130

31+
const filename = file.replace(/\\/g, '/');
32+
const dataname = filename.replace(/[./-]/g, '_');
33+
3234
const rawContent = readFileSync(path.join(cmdLine.sourcepath, file), { flag: 'r' });
3335
const md5 = createHash('md5').update(rawContent).digest('hex');
3436
summary.size += rawContent.length;
3537
if (cmdLine['no-gzip']) {
3638
sources.push({
37-
index: sourceIndex++,
38-
filename: file.replace(/\\/g, '/'),
39+
filename: filename,
40+
dataname,
3941
content: rawContent,
4042
isGzip: false,
4143
mime,
@@ -47,8 +49,8 @@ for (const file of files) {
4749
summary.gzipsize += zipContent.length;
4850
if (rawContent.length > 100 && zipContent.length < rawContent.length * 0.85) {
4951
sources.push({
50-
index: sourceIndex++,
51-
filename: file.replace(/\\/g, '/'),
52+
filename: filename,
53+
dataname,
5254
content: zipContent,
5355
isGzip: true,
5456
mime,
@@ -57,8 +59,8 @@ for (const file of files) {
5759
console.log(`✓ gzip used (${rawContent.length} -> ${zipContent.length})`);
5860
} else {
5961
sources.push({
60-
index: sourceIndex++,
61-
filename: file.replace(/\\/g, '/'),
62+
filename: filename,
63+
dataname,
6264
content: rawContent,
6365
isGzip: false,
6466
mime,

0 commit comments

Comments
 (0)