Skip to content

Commit

Permalink
feat: named document instead of index
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsabaEngine committed Aug 11, 2024
1 parent a8ffa04 commit 655adb5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
44 changes: 14 additions & 30 deletions src/cppCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { compile as handlebarsCompile } from 'handlebars';
import { cmdLine } from './commandLine';

export type cppCodeSource = {
index: number;
filename: string;
dataname: string;
mime: string;
content: Buffer;
isGzip: boolean;
Expand All @@ -23,24 +23,17 @@ const psychicTemplate = `
#include <PsychicHttpsServer.h>
{{#each sources}}
{{#if this.isDefault}}
const uint8_t dataDefaultDocument[{{this.length}}] = { {{this.bytes}} };
const uint8_t data_{{this.dataname}}[{{this.length}}] = { {{this.bytes}} };
{{#if ../isEtag}}
const char * etagDefaultDocument = "{{this.md5}}";
{{/if}}
{{else}}
const uint8_t data{{this.index}}[{{this.length}}] = { {{this.bytes}} };
{{#if ../isEtag}}
const char * etag{{this.index}} = "{{this.md5}}";
{{/if}}
const char * etag_{{this.dataname}} = "{{this.md5}}";
{{/if}}
{{/each}}
void {{methodName}}(PsychicHttpServer * server) {
{{#each sources}}
{{#if this.isDefault}}server->defaultEndpoint = {{/if}}server->on("/{{this.filename}}", HTTP_GET, [](PsychicRequest * request) {
{{#if ../isEtag}}
if (request->hasHeader("If-None-Match") && request->header("If-None-Match") == String({{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}})) {
if (request->hasHeader("If-None-Match") && request->header("If-None-Match") == String(etag_{{this.dataname}})) {
PsychicResponse response304(request);
response304.setCode(304);
return response304.send();
Expand All @@ -52,9 +45,9 @@ void {{methodName}}(PsychicHttpServer * server) {
response.addHeader("Content-Encoding", "gzip");
{{/if}}
{{#if ../isEtag}}
response.addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}});
response.addHeader("ETag", etag_{{this.dataname}});
{{/if}}
response.setContent({{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if}}, sizeof({{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if}}));
response.setContent(data_{{this.dataname}}, sizeof(data_{{this.dataname}}));
return response.send();
});
Expand All @@ -72,42 +65,33 @@ const asyncTemplate = `
#include <ESPAsyncWebServer.h>
{{#each sources}}
{{#if this.isDefault}}
const uint8_t dataDefaultDocument[{{this.length}}] = { {{this.bytes}} };
const uint8_t data_{{this.dataname}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
{{#if ../isEtag}}
const char * etagDefaultDocument = "{{this.md5}}";
{{/if}}
{{else}}
const uint8_t data{{this.index}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
{{#if ../isEtag}}
const char * etag{{this.index}} = "{{this.md5}}";
{{/if}}
const char * etag_{{this.dataname}} = "{{this.md5}}";
{{/if}}
{{/each}}
void {{methodName}}(AsyncWebServer * server) {
{{#each sources}}
ArRequestHandlerFunction {{#if this.isDefault}}funcDefaultDocument{{else}}func{{this.index}}{{/if}} = [](AsyncWebServerRequest * request) {
ArRequestHandlerFunction func_{{this.dataname}} = [](AsyncWebServerRequest * request) {
{{#if ../isEtag}}
if (request->hasHeader("If-None-Match") && request->getHeader("If-None-Match")->value() == String({{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}})) {
if (request->hasHeader("If-None-Match") && request->getHeader("If-None-Match")->value() == String(etag_{{this.dataname}})) {
request->send(304);
return;
}
{{/if}}
AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", {{#if this.isDefault}}dataDefaultDocument{{else}}data{{this.index}}{{/if}}, {{this.length}});
AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", data_{{this.dataname}}, {{this.length}});
{{#if this.isGzip}}
response->addHeader("Content-Encoding", "gzip");
{{/if}}
{{#if ../isEtag}}
response->addHeader("ETag", {{#if this.isDefault}}etagDefaultDocument{{else}}etag{{this.index}}{{/if}});
response->addHeader("ETag", etag_{{this.dataname}});
{{/if}}
request->send(response);
};
server->on("/{{this.filename}}", HTTP_GET, func_{{this.dataname}});
{{#if this.isDefault}}
server->on("/{{this.filename}}", HTTP_GET, funcDefaultDocument);
server->on("/", HTTP_GET, funcDefaultDocument);
{{else}}
server->on("/{{this.filename}}", HTTP_GET, func{{this.index}});
server->on("/", HTTP_GET, func_{{this.dataname}});
{{/if}}
{{/each}}
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const summary = {
};

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

const filename = file.replace(/\\/g, '/');
const dataname = filename.replace(/[./-]/g, '_');

const rawContent = readFileSync(path.join(cmdLine.sourcepath, file), { flag: 'r' });
const md5 = createHash('md5').update(rawContent).digest('hex');
summary.size += rawContent.length;
if (cmdLine['no-gzip']) {
sources.push({
index: sourceIndex++,
filename: file.replace(/\\/g, '/'),
filename: filename,
dataname,
content: rawContent,
isGzip: false,
mime,
Expand All @@ -47,8 +49,8 @@ for (const file of files) {
summary.gzipsize += zipContent.length;
if (rawContent.length > 100 && zipContent.length < rawContent.length * 0.85) {
sources.push({
index: sourceIndex++,
filename: file.replace(/\\/g, '/'),
filename: filename,
dataname,
content: zipContent,
isGzip: true,
mime,
Expand All @@ -57,8 +59,8 @@ for (const file of files) {
console.log(`✓ gzip used (${rawContent.length} -> ${zipContent.length})`);
} else {
sources.push({
index: sourceIndex++,
filename: file.replace(/\\/g, '/'),
filename: filename,
dataname,
content: rawContent,
isGzip: false,
mime,
Expand Down

0 comments on commit 655adb5

Please sign in to comment.