-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleWebServer.cpp
More file actions
107 lines (87 loc) · 3.07 KB
/
ExampleWebServer.cpp
File metadata and controls
107 lines (87 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <ExampleWebServer.h>
// auto-generated. will be included from dist
#include <web_assets.h>
#include <Update.h>
static const char TEXT_HTML[] = "text/html";
static const char TEXT_PLAIN[] = "text/plain";
static const char APPLICATION_JSON[] = "application/json";
static const AuthProvider authProvider;
using namespace std::placeholders;
ExampleWebServer::ExampleWebServer()
: server(RichHttpServer<RichHttpConfig>(80, authProvider))
{ }
ExampleWebServer::~ExampleWebServer() {
server.reset();
}
void ExampleWebServer::begin() {
// Register auto-generated static assets
for (auto it = WEB_ASSET_CONTENTS.begin(); it != WEB_ASSET_CONTENTS.end(); ++it) {
const char* filename = it->first;
const uint8_t* contents = it->second;
const size_t length = WEB_ASSET_LENGTHS.at(filename);
const char* contentType = WEB_ASSET_CONTENT_TYPES.at(filename);
server
.buildHandler(filename)
.on(HTTP_GET, std::bind(&ExampleWebServer::handleServeGzip_P, this, contentType, contents, length, _1));
}
server
.buildHandler("/api/v1/system")
.on(HTTP_GET, std::bind(&ExampleWebServer::handleGetSystem, this, _1))
.on(HTTP_POST, std::bind(&ExampleWebServer::handlePostSystem, this, _1));
server
.buildHandler("/firmware")
.handleOTA();
// Redirect anything prefixed with /app to index.html
server.onNotFound([this](AsyncWebServerRequest *request) {
if (request->url() == "/" || request->url().startsWith("/app")) {
_handleServeGzip_P(TEXT_HTML, INDEX_HTML_GZ, INDEX_HTML_GZ_LENGTH, request);
} else {
request->send(404);
}
});
server.clearBuilders();
server.begin();
}
void ExampleWebServer::handlePostSystem(RequestContext& request) {
JsonObject body = request.getJsonBody().as<JsonObject>();
JsonVariant command = body[F("command")];
if (command.isNull()) {
request.response.json[F("error")] = F("Command not specified");
request.response.setCode(400);
return;
}
String strCommand = command.as<String>();
if (strCommand.equalsIgnoreCase("reboot")) {
ESP.restart();
request.response.json[F("success")] = true;
} else {
request.response.json[F("error")] = F("Unhandled command");
request.response.setCode(400);
}
}
void ExampleWebServer::handleGetSystem(RequestContext& request) {
// Measure before allocating buffers
uint32_t freeHeap = ESP.getFreeHeap();
request.response.json["version"] = "1.0.0";
request.response.json["variant"] = "esp32";
request.response.json["free_heap"] = freeHeap;
request.response.json["sdk_version"] = ESP.getSdkVersion();
}
void ExampleWebServer::handleServeGzip_P(
const char* contentType,
const uint8_t* text,
size_t length,
RequestContext& request
) {
_handleServeGzip_P(contentType, text, length, request.rawRequest);
}
void ExampleWebServer::_handleServeGzip_P(
const char* contentType,
const uint8_t* text,
size_t length,
AsyncWebServerRequest* request
) {
AsyncWebServerResponse* response = request->beginResponse_P(200, contentType, text, length);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
}