mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
first commit using PsychicHttp
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
#include <ESP8266React.h>
|
||||
|
||||
#include <WWWData.h>
|
||||
|
||||
ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
|
||||
: _featureService(server)
|
||||
, _securitySettingsService(server, fs)
|
||||
, _networkSettingsService(server, fs, &_securitySettingsService)
|
||||
, _wifiScanner(server, &_securitySettingsService)
|
||||
ESP8266React::ESP8266React(PsychicHttpServer * server, FS * fs)
|
||||
: _networkSettingsService(server, fs, &_securitySettingsService)
|
||||
, _networkStatus(server, &_securitySettingsService)
|
||||
, _featureService(server)
|
||||
, _securitySettingsService(server, fs)
|
||||
, _wifiScanner(server, &_securitySettingsService)
|
||||
, _apSettingsService(server, fs, &_securitySettingsService)
|
||||
, _apStatus(server, &_securitySettingsService, &_apSettingsService)
|
||||
, _ntpSettingsService(server, fs, &_securitySettingsService)
|
||||
@@ -20,40 +18,11 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
|
||||
, _restartService(server, &_securitySettingsService)
|
||||
, _factoryResetService(server, fs, &_securitySettingsService)
|
||||
, _systemStatus(server, &_securitySettingsService) {
|
||||
// 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");
|
||||
// response->addHeader("Content-Encoding", "br"); // only works over HTTPS
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// register services
|
||||
void ESP8266React::begin() {
|
||||
_networkSettingsService.begin();
|
||||
_networkSettingsService.read([&](NetworkSettings & networkSettings) {
|
||||
if (networkSettings.enableCORS) {
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", networkSettings.CORSOrigin);
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
|
||||
}
|
||||
});
|
||||
_apSettingsService.begin();
|
||||
_ntpSettingsService.begin();
|
||||
_otaSettingsService.begin();
|
||||
@@ -61,6 +30,35 @@ void ESP8266React::begin() {
|
||||
_securitySettingsService.begin();
|
||||
}
|
||||
|
||||
// create the web server endpoints
|
||||
void ESP8266React::registerURI() {
|
||||
_featureService.registerURI();
|
||||
_authenticationService.registerURI();
|
||||
_systemStatus.registerURI();
|
||||
|
||||
_networkSettingsService.registerURI();
|
||||
_networkStatus.registerURI();
|
||||
|
||||
_apSettingsService.registerURI();
|
||||
_apStatus.registerURI();
|
||||
|
||||
_ntpSettingsService.registerURI();
|
||||
_ntpStatus.registerURI();
|
||||
|
||||
_mqttSettingsService.registerURI();
|
||||
_mqttStatus.registerURI();
|
||||
|
||||
_securitySettingsService.registerURI();
|
||||
_otaSettingsService.registerURI();
|
||||
|
||||
_restartService.registerURI();
|
||||
_factoryResetService.registerURI();
|
||||
|
||||
_wifiScanner.registerURI();
|
||||
|
||||
_uploadFileService.registerURI();
|
||||
}
|
||||
|
||||
void ESP8266React::loop() {
|
||||
_networkSettingsService.loop();
|
||||
_apSettingsService.loop();
|
||||
|
||||
Reference in New Issue
Block a user