Files
PlatformIO_Esp32C3Boilerplate/src/web/routes.cpp

49 lines
1.7 KiB
C++
Raw Normal View History

#include "../app.h"
static HTTPMethod httpMethod(const char *method) {
if (strcmp(method, "GET") == 0) return HTTP_GET;
if (strcmp(method, "POST") == 0) return HTTP_POST;
if (strcmp(method, "DELETE") == 0) return HTTP_DELETE;
if (strcmp(method, "PUT") == 0) return HTTP_PUT;
if (strcmp(method, "PATCH") == 0) return HTTP_PATCH;
return HTTP_ANY;
}
static void registerApiRoutes() {
for (size_t i = 0; i < API_DEF_COUNT; i++) {
ApiDef &api = apiDefs[i];
if (api.uploadHandler) {
server.on(api.path, httpMethod(api.method), api.handler, api.uploadHandler);
} else {
server.on(api.path, httpMethod(api.method), api.handler);
}
}
}
void registerRoutes() {
static const char *headers[] = {"Authorization", "X-Auth-Token"};
server.collectHeaders(headers, 2);
server.on("/", HTTP_GET, setupMode ? handleSetupPage : handleAdminPage);
server.on("/favicon.ico", HTTP_GET, handleFavicon);
server.on("/generate_204", HTTP_GET, handleCaptiveProbe);
server.on("/gen_204", HTTP_GET, handleCaptiveProbe);
server.on("/hotspot-detect.html", HTTP_GET, handleCaptiveProbe);
server.on("/library/test/success.html", HTTP_GET, handleCaptiveProbe);
server.on("/connecttest.txt", HTTP_GET, handleCaptiveProbe);
server.on("/ncsi.txt", HTTP_GET, handleCaptiveProbe);
server.on("/fwlink", HTTP_GET, handleCaptiveProbe);
registerApiRoutes();
server.onNotFound([]() {
if (setupMode) {
if (!handleHtmlFileRequest()) redirectToSetupPage();
} else if (server.method() == HTTP_GET && !server.uri().startsWith("/api/")) {
if (!handleHtmlFileRequest()) handleAdminPage();
} else {
sendJson(404, jsonError("Not found"));
}
});
}