-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_react.ino
More file actions
53 lines (46 loc) · 1.86 KB
/
handle_react.ino
File metadata and controls
53 lines (46 loc) · 1.86 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
// #define GZIP_ENABLE // If you use gzip, uncomment this.
String get_mime(String type) {
if (type == ".html") return "text/html";
else if (type == ".js") return "text/javascript";
else if (type == ".css") return "text/css";
else if (type == ".ico") return "image/x-ico";
else if (type == ".ttf" || type == ".otf" || type == ".woff") return "application/font-sfnt";
else if (type == ".json") return "application/json";
else if (type == ".xml") return "application/xml";
else if (type == ".txt") return "text/plain";
else if (type == ".jpg" || type == ".jpeg") return "image/jpeg";
else if (type == ".png") return "image/png";
else if (type == ".gif") return "image/gif";
else if (type == ".svg") return "image/svg+xml";
else if (type == ".webp") return "image/webp";
else if (type == ".pdf") return "application/pdf";
else if (type == ".zip") return "application/zip";
else if (type == ".mp3") return "audio/mpeg";
else if (type == ".mp4") return "video/mp4";
else if (type == ".mpeg") return "video/mpeg";
else if (type == ".webm") return "video/webm";
return "";
}
void handle_react_reqs(AsyncWebServerRequest *req) {
String full_route = req->url();
if (full_route == "/") full_route = "/index.html";
String type = full_route.substring(full_route.lastIndexOf("."));
String mime = get_mime(type);
//Serial.println("Full Route: " + full_route);
if (mime != "") {
#ifdef GZIP_ENABLE
AsyncWebServerResponse *response = req->beginResponse(LittleFS, "/webapp/" + full_route + ".gz", mime);
response->addHeader("Content-Encoding", "gzip");
req->send(response);
#else
req->send(LittleFS, "/webapp/" + full_route, mime);
#endif
} else {
req->send(404, "text/plain", "404: Not Found");
}
}
void handle_react(AsyncWebServer *server) {
server->on("/*", HTTP_GET, [](AsyncWebServerRequest *req) {
handle_react_reqs(req);
});
}