HTTPServer: serve static file from the SPIFFS
This commit is contained in:
parent
a0c5d5587d
commit
fd0d75ee16
|
@ -28,6 +28,8 @@ class HTTPServer
|
||||||
|
|
||||||
static void serverTask(void *arg);
|
static void serverTask(void *arg);
|
||||||
|
|
||||||
|
static void serveFile(String filename, httpsserver::HTTPResponse *res);
|
||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
static void handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res);
|
static void handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res);
|
||||||
static void handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res);
|
static void handleColor(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
/* HTTP Server setup and handler functions */
|
/* HTTP Server setup and handler functions */
|
||||||
|
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
|
||||||
#include "HTTPServer.h"
|
#include "HTTPServer.h"
|
||||||
|
|
||||||
#include "Fader.h"
|
#include "Fader.h"
|
||||||
|
@ -10,14 +12,28 @@ HTTPServer::HTTPServer(void)
|
||||||
m_server = new httpsserver::HTTPServer();
|
m_server = new httpsserver::HTTPServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HTTPServer::serveFile(String filename, httpsserver::HTTPResponse *res)
|
||||||
|
{
|
||||||
|
uint8_t buf[1024];
|
||||||
|
File f = SPIFFS.open(filename.c_str(), "r");
|
||||||
|
|
||||||
|
size_t nread = 1;
|
||||||
|
while(nread > 0) {
|
||||||
|
nread = f.readBytes(reinterpret_cast<char*>(buf), 1024);
|
||||||
|
if(nread <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
res->write(buf, nread);
|
||||||
|
}
|
||||||
|
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
|
||||||
void HTTPServer::handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
|
void HTTPServer::handleRoot(httpsserver::HTTPRequest *req, httpsserver::HTTPResponse *res)
|
||||||
{
|
{
|
||||||
res->setHeader("Content-Type", "text/plain");
|
serveFile("/index.html", res);
|
||||||
|
res->setHeader("Content-Type", "text/html");
|
||||||
res->println("Hello World!");
|
|
||||||
res->print("Uptime: ");
|
|
||||||
res->print(millis());
|
|
||||||
res->println(" ms");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error400(httpsserver::HTTPResponse *res, const std::string &reason)
|
static void error400(httpsserver::HTTPResponse *res, const std::string &reason)
|
||||||
|
|
Loading…
Reference in a new issue