Add feature to enable CORS during the build.

This commit is contained in:
rjwats@gmail.com
2018-02-26 22:55:58 +00:00
parent 63a639eb22
commit 85784a2535
2 changed files with 12 additions and 1 deletions

View File

@ -44,14 +44,23 @@ void setup() {
server.serveStatic("/app/", SPIFFS, "/www/app/");
// Serving all other get requests with "/www/index.htm"
// OPTIONS get a straight up 200 response
server.onNotFound([](AsyncWebServerRequest *request) {
if (request->method() == HTTP_GET) {
request->send(SPIFFS, "/www/index.html");
} else if (request->method() == HTTP_OPTIONS) {
request->send(200);
} else {
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();
}