Add static Http server example, fix wildcards
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include <experimental/filesystem>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
|
||||
struct FileCache {
|
||||
private:
|
||||
std::map<std::string_view, std::string_view> cache;
|
||||
|
||||
void cacheFile(std::string file, std::string root) {
|
||||
std::cout << "Caching file: " << file << std::endl;
|
||||
|
||||
std::ifstream fin(root + file, std::ios::binary);
|
||||
std::ostringstream oss;
|
||||
oss << fin.rdbuf();
|
||||
|
||||
if (file == "/index.html") {
|
||||
file = "/";
|
||||
}
|
||||
|
||||
char *cachedFile = (char *) malloc(oss.str().size());
|
||||
memcpy(cachedFile, oss.str().data(), oss.str().size());
|
||||
|
||||
char *key = (char *) malloc(file.length());
|
||||
memcpy(key, file.data(), file.length());
|
||||
|
||||
std::cout << "Size: " << oss.str().size() << std::endl;
|
||||
|
||||
cache[std::string_view(key, file.length())] = std::string_view(cachedFile, oss.str().size());
|
||||
}
|
||||
|
||||
public:
|
||||
FileCache(std::string root) {
|
||||
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
|
||||
cacheFile(p.path().string().substr(root.length()), root);
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view getFile(std::string_view file) {
|
||||
auto it = cache.find(file);
|
||||
|
||||
if (it == cache.end()) {
|
||||
//std::cout << "Did not find file: " << file << std::endl;
|
||||
return "<h1>Nope!</h1>";
|
||||
} else {
|
||||
//std::cout << "Did find file: " << file << std::endl;
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
/* Middleware to fill out content-type */
|
||||
inline bool hasExt(std::string_view file, std::string_view ext) {
|
||||
if (ext.size() > file.size()) {
|
||||
return false;
|
||||
}
|
||||
return std::equal(ext.rbegin(), ext.rend(), file.rbegin());
|
||||
}
|
||||
|
||||
/* This should be a filter / middleware like app.use(handler) */
|
||||
template <bool SSL>
|
||||
uWS::HttpResponse<SSL> *serveFile(uWS::HttpResponse<SSL> *res, uWS::HttpRequest *req) {
|
||||
res->writeStatus(uWS::HTTP_200_OK);
|
||||
|
||||
if (hasExt(req->getUrl(), ".svg")) {
|
||||
res->writeHeader("Content-Type", "image/svg+xml");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/* This is a static HTTP(S) server. It caches the files in given root folder and streams them by request */
|
||||
|
||||
#include <App.h>
|
||||
|
||||
/* Helpers for this example */
|
||||
#include "FileCache.h"
|
||||
#include "Middleware.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if (argc < 3 || argc > 6) {
|
||||
std::cout << "Usage: static_http_server root port [ssl_cert ssl_key ssl_passphrase]" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int port = atoi(argv[2]);
|
||||
const char *root = argv[1];
|
||||
/* Cache files of root folder */
|
||||
FileCache fileCache(root);
|
||||
|
||||
/* Either serve over HTTP or HTTPS */
|
||||
if (argc > 4) {
|
||||
/* HTTPS */
|
||||
uWS::SSLApp({
|
||||
.cert_file_name = argv[3],
|
||||
.key_file_name = argv[4],
|
||||
.passphrase = (argc == 6 ? argv[5] : nullptr)
|
||||
}).get("/*", [&fileCache](auto *res, auto *req) {
|
||||
serveFile(res, req)->write(fileCache.getFile(req->getUrl()));
|
||||
}).listen(port, [port, root](auto *token) {
|
||||
if (token) {
|
||||
std::cout << "Serving " << root << " over HTTPS a " << port << std::endl;
|
||||
}
|
||||
}).run();
|
||||
} else {
|
||||
/* HTTP */
|
||||
uWS::App().get("/*", [&fileCache](auto *res, auto *req) {
|
||||
serveFile(res, req)->write(fileCache.getFile(req->getUrl()));
|
||||
}).listen(port, [port, root](auto *token) {
|
||||
if (token) {
|
||||
std::cout << "Serving " << root << " over HTTP a " << port << std::endl;
|
||||
}
|
||||
}).run();
|
||||
}
|
||||
|
||||
std::cout << "Failed to listen to port " << port << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user