webserver

This commit is contained in:
Matthew Frost 2023-06-13 22:54:27 +02:00
parent 2c1c126dc2
commit 40b249778e

View file

@ -11,6 +11,7 @@
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
#include "hardware.h"
#include "settings.h"
class MainWebServerClass{
@ -40,6 +41,11 @@ class MainWebServerClass{
});
_server->onNotFound([&](AsyncWebServerRequest *request){
if(_authRequired){
if(!request->authenticate(_username.c_str(), _password.c_str())){
return request->requestAuthentication();
}
}
request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}");
});
@ -86,25 +92,36 @@ class MainWebServerClass{
});
#ifdef RELAY1
_server->on("/state/relay1", HTTP_GET, [] (AsyncWebServerRequest *request) {
if(!request->authenticate(http_username, http_password))
return request->requestAuthentication();
_server->on("/state/relay1", HTTP_GET, [&] (AsyncWebServerRequest *request) {
if(_authRequired){
if(!request->authenticate(_username.c_str(), _password.c_str())){
return request->requestAuthentication();
}
}
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"state\":\""+stateDoor()+"\"}");
request->send(response);
});
#endif
_server->on("/settings/get/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
_server->on("/settings/get/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
if(_authRequired){
if(!request->authenticate(_username.c_str(), _password.c_str())){
return request->requestAuthentication();
}
}
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.DoorDisabled())+"\"}");
request->send(response);
});
_server->on("/settings/set/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) {
_server->on("/settings/set/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) {
String value;
if(!request->authenticate(http_username, http_password))
return request->requestAuthentication();
if(_authRequired){
if(!request->authenticate(_username.c_str(), _password.c_str())){
return request->requestAuthentication();
}
}
if (request->hasParam("value")) {
value = request->getParam("value")->value();
if (value == "0") {