Merge pull request #4 from PlxEV/codex/redirecionar-para-index-e-navbar

Fix SPA redirect
This commit is contained in:
2025-06-07 15:05:51 +01:00
committed by GitHub

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);
if (fd == -1) {
ESP_LOGE(REST_TAG, "Failed to open file : %s", filepath);
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
return ESP_FAIL;
ESP_LOGW(REST_TAG, "Failed to open file : %s, redirecting to index", filepath);
/* Try to serve index.html for SPA routing */
strlcpy(filepath, rest_context->base_path, sizeof(filepath));
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);