More work on the ESP32C3 Boilerplate

This commit is contained in:
2026-06-27 20:00:17 -05:00
parent bb7aa5d5ca
commit 54ae1e66da
21 changed files with 833 additions and 146 deletions

View File

@@ -1,4 +1,4 @@
#include "../app.h"
#include "app.h"
static HTTPMethod httpMethod(const char *method) {
if (strcmp(method, "GET") == 0) return HTTP_GET;
@@ -9,9 +9,9 @@ static HTTPMethod httpMethod(const char *method) {
return HTTP_ANY;
}
static void registerApiRoutes() {
for (size_t i = 0; i < API_DEF_COUNT; i++) {
ApiDef &api = apiDefs[i];
static void registerApiDefs(ApiDef *defs, size_t count) {
for (size_t i = 0; i < count; i++) {
ApiDef &api = defs[i];
if (api.uploadHandler) {
server.on(api.path, httpMethod(api.method), api.handler, api.uploadHandler);
} else {
@@ -20,6 +20,28 @@ static void registerApiRoutes() {
}
}
static void registerApiRoutes() {
registerApiDefs(apiDefs, API_DEF_COUNT);
registerApiDefs(customApiDefs, CUSTOM_API_DEF_COUNT);
}
static String requestMethodName() {
switch (server.method()) {
case HTTP_GET:
return "GET";
case HTTP_POST:
return "POST";
case HTTP_DELETE:
return "DELETE";
case HTTP_PUT:
return "PUT";
case HTTP_PATCH:
return "PATCH";
default:
return "UNKNOWN";
}
}
void registerRoutes() {
static const char *headers[] = {"Authorization", "X-Auth-Token"};
server.collectHeaders(headers, 2);
@@ -38,10 +60,16 @@ void registerRoutes() {
server.onNotFound([]() {
if (setupMode) {
if (!handleHtmlFileRequest()) redirectToSetupPage();
if (server.uri().startsWith("/api/")) {
appendLog(LOG_SECURITY_AUDIT, "undefined URL or method " + requestMethodName() + " " + server.uri());
sendJson(404, jsonError("Not found"));
} else if (!handleHtmlFileRequest()) {
redirectToSetupPage();
}
} else if (server.method() == HTTP_GET && !server.uri().startsWith("/api/")) {
if (!handleHtmlFileRequest()) handleAdminPage();
} else {
appendLog(LOG_SECURITY_AUDIT, "undefined URL or method " + requestMethodName() + " " + server.uri());
sendJson(404, jsonError("Not found"));
}
});