🐛 Handle spa

This commit is contained in:
Rune Harlyk
2026-01-31 18:57:34 +01:00
committed by Rune Harlyk
parent 13300aa9e0
commit f4f8035f37
6 changed files with 45 additions and 65 deletions
+23
View File
@@ -1,4 +1,14 @@
#include "www_mount.hpp"
#include <cstring>
static const WebAsset* findAsset(const char* uri) {
for (size_t i = 0; i < WWW_ASSETS_COUNT; i++) {
if (strcmp(WWW_ASSETS[i].uri, uri) == 0) {
return &WWW_ASSETS[i];
}
}
return nullptr;
}
static esp_err_t web_send(httpd_req_t* req, const WebAsset& asset) {
httpd_resp_set_status(req, "200 OK");
@@ -23,3 +33,16 @@ void mountStaticAssets(WebServer& server) {
server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); });
}
}
void mountSpaFallback(WebServer& server) {
const WebAsset* indexAsset = findAsset(WWW_OPT.default_uri);
if (indexAsset) {
server.on("/*", HTTP_GET, [indexAsset](httpd_req_t* req) {
if (strncmp(req->uri, "/api/", 5) == 0) {
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Not found");
return ESP_FAIL;
}
return web_send(req, *indexAsset);
});
}
}