рефактор статических файлов, добавлено удобное API и поддержка кеширования

This commit is contained in:
2024-11-04 14:17:34 +03:00
parent 82b433c447
commit 0b794fac40
6 changed files with 217 additions and 117 deletions

View File

@@ -1,6 +1,9 @@
#include "resource.h"
#include <boost/log/trivial.hpp>
#include <fstream>
#include <utility>
#include "../../dependencies/control_system/common/protocol_commands.h"
static void loadFile(const std::string& path, std::vector<char>& content) {
std::ifstream is(path, std::ios::in | std::ios::binary);
@@ -19,43 +22,59 @@ static void loadFile(const std::string& path, std::vector<char>& content) {
}
}
http::resource::StaticFileResource::StaticFileResource(const std::string &path, const std::string &filePath, server::mime_types::Mime type): type(type) {
this->path = path;
#ifdef USE_DEBUG
BOOST_LOG_TRIVIAL(info) << "Skip loading file " << filePath << " (http path: " << path << ")";
this->filePath = filePath;
#else
BOOST_LOG_TRIVIAL(info) << "Load file " << filePath << " (http path: " << path << ")";
loadFile(filePath, this->content);
#endif
}
http::resource::BasicResource::BasicResource(std::string path): path(std::move(path)) {}
void http::resource::StaticFileResource::handle(const server::Request &req, server::Reply &rep) {
if (req.method != "GET") {
stockReply(server::bad_request, rep);
return;
http::resource::StaticFileFactory::StaticFileDef::StaticFileDef(std::string path, server::mime_types::Mime type, bool allowCache): path(std::move(path)), type(type), allowCache(allowCache) {
#ifdef USE_DEBUG
if (allowCache) {
BOOST_LOG_TRIVIAL(info) << "Load static file " << this->path;
loadFile(this->path, this->content);
} else {
BOOST_LOG_TRIVIAL(info) << "Skip loading static file " << this->path;
}
// TODO сделать поддержку range
#ifdef USE_DEBUG
BOOST_LOG_TRIVIAL(debug) << "Reload file " << filePath << " (http path: " << path << ")";
loadFile(this->filePath, rep.content);
#else
rep.content.clear();
rep.content.insert(rep.content.end(), this->content.begin(), this->content.end());
BOOST_LOG_TRIVIAL(info) << "Load static file " << this->path;
loadFile(this->path, this->content);
#endif
rep.status = server::ok;
rep.headers.clear();
// TODO сделать cache control
rep.headers.push_back({.name = "Content-Type", .value = toString(this->type)});
}
http::resource::StaticFileFactory::StaticFileDef::~StaticFileDef() = default;
http::resource::StaticFileFactory::StaticFileFactory() = default;
void http::resource::StaticFileFactory::registerFile(const std::string &path, server::mime_types::Mime type, bool allowCache) {
this->files.emplace_back(path, type, allowCache);
}
http::resource::StaticFileResource::~StaticFileResource() = default;
http::resource::GenericResource::GenericResource(const std::string &path, const respGenerator &generator): generator_(generator) {
this->path = path;
void http::resource::StaticFileFactory::serve(const std::string &path, server::Reply &rep) {
for (auto& f: this->files) {
if (f.path == path) {
#ifdef USE_DEBUG
if (f.allowCache) {
rep.content.clear();
rep.content.insert(rep.content.end(), f.content.begin(), f.content.end());
} else {
BOOST_LOG_TRIVIAL(debug) << "Reload file " << path << " (http path: " << path << ")";
loadFile(f.path, rep.content);
}
#else
rep.content.clear();
rep.content.insert(rep.content.end(), f.content.begin(), f.content.end());
#endif
rep.status = server::ok;
// rep.headers.clear();
rep.headers.push_back({.name = "Content-Type", .value = server::mime_types::toString(f.type)});
if (f.allowCache) {
rep.headers.push_back({.name = "Cache-Control", .value = "max-age=86400"}); // сутки, думаю хватит
}
return;
}
}
}
http::resource::StaticFileFactory::~StaticFileFactory() = default;
http::resource::GenericResource::GenericResource(const std::string &path, respGenerator generator): BasicResource(path), generator_(std::move(generator)) {}
void http::resource::GenericResource::handle(const server::Request &req, server::Reply &rep) {
this->generator_(req, rep);
}