Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

  1. #include <ESP8266React.h>
  2. ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
  3. _featureService(server),
  4. _securitySettingsService(server, fs),
  5. _wifiSettingsService(server, fs, &_securitySettingsService),
  6. _wifiScanner(server, &_securitySettingsService),
  7. _wifiStatus(server, &_securitySettingsService),
  8. _apSettingsService(server, fs, &_securitySettingsService),
  9. _apStatus(server, &_securitySettingsService, &_apSettingsService),
  10. #if FT_ENABLED(FT_NTP)
  11. _ntpSettingsService(server, fs, &_securitySettingsService),
  12. _ntpStatus(server, &_securitySettingsService),
  13. #endif
  14. #if FT_ENABLED(FT_OTA)
  15. _otaSettingsService(server, fs, &_securitySettingsService),
  16. #endif
  17. #if FT_ENABLED(FT_MQTT)
  18. _mqttSettingsService(server, fs, &_securitySettingsService),
  19. _mqttStatus(server, &_mqttSettingsService, &_securitySettingsService),
  20. #endif
  21. #if FT_ENABLED(FT_SECURITY)
  22. _authenticationService(server, &_securitySettingsService),
  23. #endif
  24. _restartService(server, &_securitySettingsService),
  25. _factoryResetService(server, fs, &_securitySettingsService),
  26. _systemStatus(server, &_securitySettingsService) {
  27. #ifdef PROGMEM_WWW
  28. // Serve static resources from PROGMEM
  29. WWWData::registerRoutes(
  30. [server, this](const String& uri, const String& contentType, const uint8_t* content, size_t len) {
  31. ArRequestHandlerFunction requestHandler = [contentType, content, len](AsyncWebServerRequest* request) {
  32. AsyncWebServerResponse* response = request->beginResponse_P(200, contentType, content, len);
  33. response->addHeader("Content-Encoding", "gzip");
  34. request->send(response);
  35. };
  36. server->on(uri.c_str(), HTTP_GET, requestHandler);
  37. // Serving non matching get requests with "/index.html"
  38. // OPTIONS get a straight up 200 response
  39. if (uri.equals("/index.html")) {
  40. server->onNotFound([requestHandler](AsyncWebServerRequest* request) {
  41. if (request->method() == HTTP_GET) {
  42. requestHandler(request);
  43. } else if (request->method() == HTTP_OPTIONS) {
  44. request->send(200);
  45. } else {
  46. request->send(404);
  47. }
  48. });
  49. }
  50. });
  51. #else
  52. // Serve static resources from /www/
  53. server->serveStatic("/js/", *fs, "/www/js/");
  54. server->serveStatic("/css/", *fs, "/www/css/");
  55. server->serveStatic("/fonts/", *fs, "/www/fonts/");
  56. server->serveStatic("/app/", *fs, "/www/app/");
  57. server->serveStatic("/favicon.ico", *fs, "/www/favicon.ico");
  58. // Serving all other get requests with "/www/index.htm"
  59. // OPTIONS get a straight up 200 response
  60. server->onNotFound([](AsyncWebServerRequest* request) {
  61. if (request->method() == HTTP_GET) {
  62. request->send(SPIFFS, "/www/index.html");
  63. } else if (request->method() == HTTP_OPTIONS) {
  64. request->send(200);
  65. } else {
  66. request->send(404);
  67. }
  68. });
  69. #endif
  70. // Disable CORS if required
  71. #if defined(ENABLE_CORS)
  72. DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN);
  73. DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
  74. DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true");
  75. #endif
  76. }
  77. void ESP8266React::begin() {
  78. _wifiSettingsService.begin();
  79. _apSettingsService.begin();
  80. #if FT_ENABLED(FT_NTP)
  81. _ntpSettingsService.begin();
  82. #endif
  83. #if FT_ENABLED(FT_OTA)
  84. _otaSettingsService.begin();
  85. #endif
  86. #if FT_ENABLED(FT_MQTT)
  87. _mqttSettingsService.begin();
  88. #endif
  89. #if FT_ENABLED(FT_SECURITY)
  90. _securitySettingsService.begin();
  91. #endif
  92. }
  93. void ESP8266React::loop() {
  94. _wifiSettingsService.loop();
  95. _apSettingsService.loop();
  96. #if FT_ENABLED(FT_OTA)
  97. _otaSettingsService.loop();
  98. #endif
  99. #if FT_ENABLED(FT_MQTT)
  100. _mqttSettingsService.loop();
  101. #endif
  102. }