From 6f74811f39649dc1aa03387c35705a53bf11ab5d Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Tue, 13 Jun 2023 21:11:23 +0200 Subject: [PATCH] first seperation of web server --- include/main.h | 3 ++ include/mainwebserver.h | 95 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 32 +------------- 3 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 include/mainwebserver.h diff --git a/include/main.h b/include/main.h index fcf8c6b..792de14 100644 --- a/include/main.h +++ b/include/main.h @@ -28,6 +28,9 @@ #ifdef WEB_OTA_UPDATE #include #endif + + #include "mainwebserver.h" + #endif #ifdef BUZZER diff --git a/include/mainwebserver.h b/include/mainwebserver.h new file mode 100644 index 0000000..a8177ca --- /dev/null +++ b/include/mainwebserver.h @@ -0,0 +1,95 @@ +#ifndef mainwebserver_h +#define mainwebserver_h + +#include "Arduino.h" +#include "stdlib_noniso.h" +#include "WiFi.h" +#include "AsyncTCP.h" +#include "Update.h" +#include "esp_int_wdt.h" +#include "esp_task_wdt.h" +#include "ESPAsyncWebServer.h" +#include "SPIFFS.h" +#include "hardware.h" + +class MainWebServerClass{ + + public: + + void begin(AsyncWebServer *server, const char* username = "", const char* password = ""){ + _server = server; + + if(strlen(username) > 0){ + _authRequired = true; + _username = username; + _password = password; + }else{ + _authRequired = false; + _username = ""; + _password = ""; + } + + _server->on("/test/identity", HTTP_GET, [&](AsyncWebServerRequest *request){ + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + request->send(200, "application/json"); + }); + + _server->on("/", HTTP_GET, [&](AsyncWebServerRequest *request){ + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + + request->send(SPIFFS, "/index.html", String(), false, processor); + }); + + _server->onNotFound([&](AsyncWebServerRequest *request){ + request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}"); + }); + + } + + // deprecated, keeping for backward compatibility + void loop() { + } + + private: + AsyncWebServer *_server; + String _username = ""; + String _password = ""; + bool _authRequired = false; + + static String outputState(int output){ + if(digitalRead(output)){ + return "checked"; + } + else { + return ""; + } + } + + // Replaces placeholder with button section in your web page + static String processor(const String& var){ + //Serial.println(var); + if(var == "BUTTONPLACEHOLDER"){ + String buttons = ""; + + #ifdef RELAY1 + buttons += "

Output - Relay 1

"; + #endif + + return buttons; + } + return String(); + } + + +}; + +MainWebServerClass MainWebServer; +#endif diff --git a/src/main.cpp b/src/main.cpp index 5fea09d..17385b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,29 +96,7 @@ void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){ #endif #ifdef WEB_SERVER - String outputState(int output){ - if(digitalRead(output)){ - return "checked"; - } - else { - return ""; - } -} -// Replaces placeholder with button section in your web page -String processor(const String& var){ - //Serial.println(var); - if(var == "BUTTONPLACEHOLDER"){ - String buttons = ""; - - #ifdef RELAY1 - buttons += "

Output - Relay 1

"; - #endif - - return buttons; - } - return String(); -} #ifdef LOCAL_ACL #ifdef LOCAL_ACL_API @@ -331,15 +309,6 @@ void setup() { #ifdef WEB_SERVER // Route for root / web page - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ - if(!request->authenticate(http_username, http_password)) - return request->requestAuthentication(); - request->send(SPIFFS, "/index.html", String(), false, processor); - }); - - server.onNotFound([](AsyncWebServerRequest *request){ - request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}"); - }); #ifdef RELAY1 @@ -433,6 +402,7 @@ void setup() { } }); + MainWebServer.begin(&server, http_username, http_password); #ifdef LOCAL_ACL