Skip to content

Commit

Permalink
mathieucarbou/ESPAsyncWebServer @ 3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Sep 14, 2024
1 parent f4d4e05 commit ec10e75
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
2 changes: 2 additions & 0 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
extern AsyncUDP udp;
extern AsyncWebServer webServer;
extern AsyncWebSocket wsDebugPID;
extern AuthenticationMiddleware authMiddleware;
extern LoggingMiddleware loggingMiddleware;
extern ESPDash dashboard;

extern Mycila::Config config;
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ lib_deps =
olikraus/U8g2 @ 2.35.25
robtillaart/CRC @ 1.0.3
mathieucarbou/AsyncTCP @ 3.2.5
mathieucarbou/ESPAsyncWebServer @ 3.2.4
mathieucarbou/ESPAsyncWebServer @ 3.3.0
mathieucarbou/MycilaConfig @ 3.0.1
mathieucarbou/MycilaDS18 @ 4.1.0
mathieucarbou/MycilaESPConnect @ 6.0.0
Expand Down
2 changes: 0 additions & 2 deletions src/init/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ Mycila::Task initConfigTask("Init Config", [](void* params) {
WebSerial.setTitle((Mycila::AppInfo.name + " Web Console").c_str());
WebSerial.setInput(false);
#endif
WebSerial.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD).c_str());
WebSerial.begin(&webServer, "/console");
logger.forwardTo(&WebSerial);

// Dashboard
dashboard.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD).c_str());
#ifdef APP_MODEL_PRO
dashboard.setTitle(Mycila::AppInfo.nameModel.c_str());
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/init/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Mycila::Task initLoggingTask("Init Logging", [](void* params) {

const bool debug = config.getBool(KEY_ENABLE_DEBUG);

loggingMiddleware.setEnabled(debug);

if (debug) {
logger.setLevel(debug ? ARDUHAL_LOG_LEVEL_DEBUG : ARDUHAL_LOG_LEVEL_INFO);
esp_log_level_set("*", static_cast<esp_log_level_t>(logger.getLevel()));
Expand Down
17 changes: 12 additions & 5 deletions src/init/HTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ extern const uint8_t config_html_gz_end[] asm("_binary__pio_data_config_html_gz_
Mycila::Task initWebTask("Init Web", [](void* params) {
logger.info(TAG, "Initializing HTTP Endpoints");

loggingMiddleware.setOutput(Serial);

authMiddleware.setAuthType(AuthenticationMiddleware::AuthType::AUTH_DIGEST);
authMiddleware.setRealm("YaSolR");
authMiddleware.setUsername(YASOLR_ADMIN_USERNAME);
authMiddleware.setPassword(config.get(KEY_ADMIN_PASSWORD).c_str());

webServer.addMiddleware(&loggingMiddleware);
webServer.addMiddleware(&authMiddleware);

webServer.rewrite("/dash/assets/logo/mini", "/logo-icon");
webServer.rewrite("/dash/assets/logo/large", "/logo");
webServer.rewrite("/dash/assets/logo", "/logo");
Expand Down Expand Up @@ -49,19 +59,16 @@ Mycila::Task initWebTask("Init Web", [](void* params) {
AsyncWebServerResponse* response = request->beginResponse(200, "text/html", config_html_gz_start, config_html_gz_end - config_html_gz_start);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/timezones", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse(true);
Mycila::NTP.timezonesToJsonArray(response->getRoot().as<JsonArray>());
response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

wsDebugPID.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
wsDebugPID.onEvent([](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
if (type == WS_EVT_CONNECT) {
logger.info(TAG, "Websocket client connected to: /ws/pid/csv");
Expand Down
57 changes: 19 additions & 38 deletions src/init/REST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {

response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// config

Expand All @@ -157,8 +156,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
AsyncWebServerResponse* response = request->beginResponse(200, "text/plain", config.backup());
response->addHeader("Content-Disposition", "attachment; filename=\"config.txt\"");
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on(
Expand All @@ -181,8 +179,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
request->_tempFile.write(data, len);
if (final)
request->_tempFile.close();
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on(
Expand All @@ -207,8 +204,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
request->_tempFile.write(data, len);
if (final)
request->_tempFile.close();
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/config", HTTP_POST, [](AsyncWebServerRequest* request) {
Expand All @@ -222,40 +218,35 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
}
request->send(200);
config.set(settings);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/config", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse(false);
config.toJson(response->getRoot());
response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// system

webServer
.on("/api/system/restart", HTTP_ANY, [](AsyncWebServerRequest* request) {
restartTask.resume();
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/system/safeboot", HTTP_ANY, [](AsyncWebServerRequest* request) {
safeBootTask.resume();
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/system/reset", HTTP_ANY, [](AsyncWebServerRequest* request) {
resetTask.resume();
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/system", HTTP_GET, [](AsyncWebServerRequest* request) {
Expand All @@ -264,8 +255,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
systemInfoToJson(root);
response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// grid

Expand All @@ -278,8 +268,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
Mycila::Grid::toJson(root, metrics);
response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// router relays

Expand All @@ -294,8 +283,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
routerRelay1.tryRelayState(false, duration);
}
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/router/relay2", HTTP_POST, [](AsyncWebServerRequest* request) {
Expand All @@ -308,8 +296,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
routerRelay2.tryRelayState(false, duration);
}
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// router dimmers

Expand All @@ -318,16 +305,14 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
if (request->hasParam("duty_cycle", true))
output1.setDimmerDutyCycle(request->getParam("duty_cycle", true)->value().toFloat() / 100);
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/router/output2/dimmer", HTTP_POST, [](AsyncWebServerRequest* request) {
if (request->hasParam("duty_cycle", true))
output2.setDimmerDutyCycle(request->getParam("duty_cycle", true)->value().toFloat() / 100);
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

// router bypass

Expand All @@ -341,8 +326,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
output1.setBypassOff();
}
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/router/output2/bypass", HTTP_POST, [](AsyncWebServerRequest* request) {
Expand All @@ -354,8 +338,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {
output2.setBypassOff();
}
request->send(200);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api/router", HTTP_GET, [](AsyncWebServerRequest* request) {
Expand Down Expand Up @@ -391,8 +374,7 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {

response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});

webServer
.on("/api", HTTP_GET, [](AsyncWebServerRequest* request) {
Expand All @@ -414,6 +396,5 @@ Mycila::Task initRestApiTask("Init REST API", [](void* params) {

response->setLength();
request->send(response);
})
.setAuthentication(YASOLR_ADMIN_USERNAME, config.get(KEY_ADMIN_PASSWORD));
});
});
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Mycila::CircularBuffer<float, 50> udpMessageRateBuffer;

AsyncWebServer webServer(80);
AsyncWebSocket wsDebugPID("/ws/pid/csv");
AuthenticationMiddleware authMiddleware;
LoggingMiddleware loggingMiddleware;
ESPDash dashboard = ESPDash(&webServer, "/dashboard", false);
Mycila::ESPConnect espConnect(webServer);

Expand Down

0 comments on commit ec10e75

Please sign in to comment.