Add feature to enable CORS during the build.
This commit is contained in:
parent
63a639eb22
commit
85784a2535
@ -14,7 +14,9 @@ framework = arduino
|
|||||||
;upload_flags = --port=8266 --auth=esp-react
|
;upload_flags = --port=8266 --auth=esp-react
|
||||||
;upload_port = 192.168.0.6
|
;upload_port = 192.168.0.6
|
||||||
board_f_cpu = 160000000L
|
board_f_cpu = 160000000L
|
||||||
build_flags= -D NO_GLOBAL_ARDUINOOTA
|
build_flags=
|
||||||
|
-D NO_GLOBAL_ARDUINOOTA
|
||||||
|
; -D ENABLE_CORS
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/PaulStoffregen/Time
|
https://github.com/PaulStoffregen/Time
|
||||||
https://github.com/gmag11/NtpClient
|
https://github.com/gmag11/NtpClient
|
||||||
|
@ -44,14 +44,23 @@ void setup() {
|
|||||||
server.serveStatic("/app/", SPIFFS, "/www/app/");
|
server.serveStatic("/app/", SPIFFS, "/www/app/");
|
||||||
|
|
||||||
// Serving all other get requests with "/www/index.htm"
|
// Serving all other get requests with "/www/index.htm"
|
||||||
|
// OPTIONS get a straight up 200 response
|
||||||
server.onNotFound([](AsyncWebServerRequest *request) {
|
server.onNotFound([](AsyncWebServerRequest *request) {
|
||||||
if (request->method() == HTTP_GET) {
|
if (request->method() == HTTP_GET) {
|
||||||
request->send(SPIFFS, "/www/index.html");
|
request->send(SPIFFS, "/www/index.html");
|
||||||
|
} else if (request->method() == HTTP_OPTIONS) {
|
||||||
|
request->send(200);
|
||||||
} else {
|
} else {
|
||||||
request->send(404);
|
request->send(404);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Disable CORS if required
|
||||||
|
#if defined(ENABLE_CORS)
|
||||||
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "*");
|
||||||
|
#endif
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user