Serve index fallback for missing files

This commit is contained in:
2025-06-07 15:05:06 +01:00
parent eee88a6ffb
commit 9b480e6c05

View File

@@ -118,10 +118,16 @@ static esp_err_t rest_common_get_handler(httpd_req_t *req)
} }
int fd = open(filepath, O_RDONLY, 0); int fd = open(filepath, O_RDONLY, 0);
if (fd == -1) { if (fd == -1) {
ESP_LOGE(REST_TAG, "Failed to open file : %s", filepath); ESP_LOGW(REST_TAG, "Failed to open file : %s, redirecting to index", filepath);
/* Respond with 500 Internal Server Error */ /* Try to serve index.html for SPA routing */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); strlcpy(filepath, rest_context->base_path, sizeof(filepath));
return ESP_FAIL; strlcat(filepath, "/index.html", sizeof(filepath));
fd = open(filepath, O_RDONLY, 0);
if (fd == -1) {
ESP_LOGE(REST_TAG, "Failed to open index file : %s", filepath);
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File not found");
return ESP_FAIL;
}
} }
set_content_type_from_file(req, filepath); set_content_type_from_file(req, filepath);