Serve index fallback for missing files
This commit is contained in:
@@ -118,11 +118,17 @@ 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));
|
||||||
|
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;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
set_content_type_from_file(req, filepath);
|
set_content_type_from_file(req, filepath);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user