53 lines
1.5 KiB
C++
Raw Normal View History

2021-06-23 16:44:37 +03:00
#include "server.hpp"
#include <FS.h>
#define DBG_OUTPUT_PORT Serial
ESP8266WebServer* serv = NULL;
String getContentType(String filename){
if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path ){
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.html";
2021-06-23 16:44:37 +03:00
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if(LittleFS.exists(pathWithGz) || LittleFS.exists(path)){
if(LittleFS.exists(pathWithGz))
path += ".gz";
File file = LittleFS.open(path, "r");
file.close();
return true;
}
return false;
}
void handleHours(){
}
void server_setup(){
LittleFS.begin();
server.on("/hour", HTTP_GET, handleHours);
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
}