2019-07-14 21:13:26 +00:00
|
|
|
#include <ESP8266React.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
|
|
|
|
_securitySettingsService(server, fs),
|
|
|
|
_wifiSettingsService(server, fs, &_securitySettingsService),
|
|
|
|
_apSettingsService(server, fs, &_securitySettingsService),
|
|
|
|
_ntpSettingsService(server, fs, &_securitySettingsService),
|
|
|
|
_otaSettingsService(server, fs, &_securitySettingsService),
|
2020-05-14 22:23:45 +00:00
|
|
|
_mqttSettingsService(server, fs, &_securitySettingsService),
|
2019-12-03 23:16:06 +00:00
|
|
|
_restartService(server, &_securitySettingsService),
|
2020-05-19 23:32:49 +00:00
|
|
|
_factoryResetService(server, fs, &_securitySettingsService),
|
2019-12-03 23:16:06 +00:00
|
|
|
_authenticationService(server, &_securitySettingsService),
|
|
|
|
_wifiScanner(server, &_securitySettingsService),
|
|
|
|
_wifiStatus(server, &_securitySettingsService),
|
|
|
|
_ntpStatus(server, &_securitySettingsService),
|
2020-05-30 08:47:24 +00:00
|
|
|
_apStatus(server, &_securitySettingsService, &_apSettingsService),
|
2020-05-14 22:23:45 +00:00
|
|
|
_mqttStatus(server, &_mqttSettingsService, &_securitySettingsService),
|
2019-12-03 23:16:06 +00:00
|
|
|
_systemStatus(server, &_securitySettingsService) {
|
2019-12-29 17:54:12 +00:00
|
|
|
#ifdef PROGMEM_WWW
|
|
|
|
// Serve static resources from PROGMEM
|
|
|
|
WWWData::registerRoutes(
|
|
|
|
[server, this](const String& uri, const String& contentType, const uint8_t* content, size_t len) {
|
|
|
|
ArRequestHandlerFunction requestHandler = [contentType, content, len](AsyncWebServerRequest* request) {
|
|
|
|
AsyncWebServerResponse* response = request->beginResponse_P(200, contentType, content, len);
|
|
|
|
response->addHeader("Content-Encoding", "gzip");
|
|
|
|
request->send(response);
|
|
|
|
};
|
|
|
|
server->on(uri.c_str(), HTTP_GET, requestHandler);
|
|
|
|
// Serving non matching get requests with "/index.html"
|
|
|
|
// OPTIONS get a straight up 200 response
|
|
|
|
if (uri.equals("/index.html")) {
|
|
|
|
server->onNotFound([requestHandler](AsyncWebServerRequest* request) {
|
|
|
|
if (request->method() == HTTP_GET) {
|
|
|
|
requestHandler(request);
|
|
|
|
} else if (request->method() == HTTP_OPTIONS) {
|
|
|
|
request->send(200);
|
|
|
|
} else {
|
|
|
|
request->send(404);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#else
|
2019-09-28 20:29:46 +00:00
|
|
|
// Serve static resources from /www/
|
2020-05-25 10:30:13 +00:00
|
|
|
server->serveStatic("/js/", *fs, "/www/js/");
|
|
|
|
server->serveStatic("/css/", *fs, "/www/css/");
|
|
|
|
server->serveStatic("/fonts/", *fs, "/www/fonts/");
|
|
|
|
server->serveStatic("/app/", *fs, "/www/app/");
|
|
|
|
server->serveStatic("/favicon.ico", *fs, "/www/favicon.ico");
|
2019-07-14 21:13:26 +00:00
|
|
|
// Serving all other get requests with "/www/index.htm"
|
|
|
|
// OPTIONS get a straight up 200 response
|
2019-12-03 23:16:06 +00:00
|
|
|
server->onNotFound([](AsyncWebServerRequest* request) {
|
2019-07-14 21:13:26 +00:00
|
|
|
if (request->method() == HTTP_GET) {
|
|
|
|
request->send(SPIFFS, "/www/index.html");
|
|
|
|
} else if (request->method() == HTTP_OPTIONS) {
|
|
|
|
request->send(200);
|
|
|
|
} else {
|
|
|
|
request->send(404);
|
|
|
|
}
|
|
|
|
});
|
2019-12-29 17:54:12 +00:00
|
|
|
#endif
|
2019-07-14 21:13:26 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
// Disable CORS if required
|
|
|
|
#if defined(ENABLE_CORS)
|
2019-07-14 21:13:26 +00:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN);
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
|
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
|
2019-12-03 23:16:06 +00:00
|
|
|
#endif
|
2019-07-14 21:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 20:28:24 +00:00
|
|
|
void ESP8266React::begin() {
|
|
|
|
_securitySettingsService.begin();
|
|
|
|
_wifiSettingsService.begin();
|
|
|
|
_apSettingsService.begin();
|
|
|
|
_ntpSettingsService.begin();
|
|
|
|
_otaSettingsService.begin();
|
2020-05-14 22:23:45 +00:00
|
|
|
_mqttSettingsService.begin();
|
2019-09-30 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
void ESP8266React::loop() {
|
2019-07-14 21:13:26 +00:00
|
|
|
_wifiSettingsService.loop();
|
|
|
|
_apSettingsService.loop();
|
|
|
|
_otaSettingsService.loop();
|
2020-05-14 22:23:45 +00:00
|
|
|
_mqttSettingsService.loop();
|
2019-07-14 21:13:26 +00:00
|
|
|
}
|