54 lines
2.0 KiB
C++
54 lines
2.0 KiB
C++
|
|
#include "../app.h"
|
||
|
|
|
||
|
|
#include <LittleFS.h>
|
||
|
|
|
||
|
|
void handleSettingsGet() {
|
||
|
|
if (!authorize("/api/settings", "GET")) return;
|
||
|
|
String out = "\"settings\":{\"logLevel\":" + String(currentLogLevel) + ",\"maxLogBytes\":" + String(maxLogBytes) + ",\"updateUrl\":\"" + jsonEscape(updateUrl) + "\",\"ledInverted\":" + String(ledInverted ? "true" : "false") + ",\"ledBrightness\":" + String(ledBrightness) + "}";
|
||
|
|
sendJson(200, jsonOk(out));
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleSettingsPost() {
|
||
|
|
if (!authorize("/api/settings", "POST")) return;
|
||
|
|
String body = requestBody();
|
||
|
|
currentLogLevel = (LogLevel)constrain(jsonIntValue(body, "logLevel", currentLogLevel), 0, 3);
|
||
|
|
maxLogBytes = constrain(jsonIntValue(body, "maxLogBytes", maxLogBytes), 4096, 128 * 1024);
|
||
|
|
ledInverted = jsonBoolValue(body, "ledInverted", ledInverted);
|
||
|
|
ledBrightness = constrain(jsonIntValue(body, "ledBrightness", ledBrightness), 0, 100);
|
||
|
|
updateUrl = jsonStringValue(body, "updateUrl", updateUrl);
|
||
|
|
prefs.putUChar("logLevel", currentLogLevel);
|
||
|
|
prefs.putUInt("logMax", maxLogBytes);
|
||
|
|
prefs.putBool("ledInv", ledInverted);
|
||
|
|
prefs.putUChar("ledBright", ledBrightness);
|
||
|
|
prefs.putString("updateUrl", updateUrl);
|
||
|
|
applyLed();
|
||
|
|
sendJson(200, jsonOk());
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleLogsGet() {
|
||
|
|
if (!authorize("/api/logs", "GET")) return;
|
||
|
|
File f = LittleFS.open("/logs.txt", "r");
|
||
|
|
String logs = f ? f.readString() : "";
|
||
|
|
if (f) f.close();
|
||
|
|
sendJson(200, jsonOk("\"logs\":\"" + jsonEscape(logs) + "\""));
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleLogsClear() {
|
||
|
|
if (!authorize("/api/logs/clear", "POST")) return;
|
||
|
|
LittleFS.remove("/logs.txt");
|
||
|
|
sendJson(200, jsonOk());
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleCertGet() {
|
||
|
|
if (!authorize("/api/cert", "GET")) return;
|
||
|
|
sendJson(200, jsonOk("\"certificate\":\"" + jsonEscape(prefString("cert", "")) + "\""));
|
||
|
|
}
|
||
|
|
|
||
|
|
void handleCertPost() {
|
||
|
|
if (!authorize("/api/cert", "POST")) return;
|
||
|
|
String cert = jsonStringValue(requestBody(), "certificate");
|
||
|
|
prefs.putString("cert", cert);
|
||
|
|
appendLog(LOG_INFO, "HTTPS certificate material saved");
|
||
|
|
sendJson(200, jsonOk("\"note\":\"certificate is stored for applications that enable TLS termination\""));
|
||
|
|
}
|