cleanup of source + build system
* seperated most parts into headers & source files * seperated anthracite into libanthracite and anthracite-bin for modules * built demo webAPI module * rewrote some code to do things in the * changed cmakelists to not build in src directory
This commit is contained in:
parent
c4540a1397
commit
54d82b8c66
26 changed files with 607 additions and 378 deletions
|
|
@ -1,7 +1,10 @@
|
|||
#include <memory>
|
||||
#pragma once
|
||||
|
||||
#include "../http/http_request.cpp"
|
||||
#include "../http/http_response.cpp"
|
||||
#include <memory>
|
||||
#include "../http/request.hpp"
|
||||
#include "../http/response.hpp"
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
class backend {
|
||||
public:
|
||||
|
|
@ -11,5 +14,7 @@ public:
|
|||
backend& operator = (backend const&) = delete;
|
||||
backend(backend&&) = delete;
|
||||
backend& operator=(backend&&) = delete;
|
||||
virtual std::unique_ptr<http_response> handle_request(http_request& req) = 0;
|
||||
virtual std::unique_ptr<http::response> handle_request(http::request& req) = 0;
|
||||
};
|
||||
|
||||
};
|
||||
|
|
@ -1,26 +1,40 @@
|
|||
#include "./file_backend.hpp"
|
||||
#include "../log/log.hpp"
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include "backend.cpp"
|
||||
|
||||
class file_backend : public backend {
|
||||
private:
|
||||
std::unordered_map<std::string,std::string> file_cache;
|
||||
std::string file_dir;
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
|
||||
std::unique_ptr<http_response> handle_request_cache(http_request& req) {
|
||||
std::unique_ptr<http::response> file_backend::handle_request_cache(http::request& req) {
|
||||
std::string filename = req.path() == "/" ? "/index.html" : req.path();
|
||||
filename = file_dir + filename;
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
||||
int status = http_status_codes::OK;
|
||||
int status = http::status_codes::OK;
|
||||
if (file_info == file_cache.end()) {
|
||||
return handle_error(http_status_codes::NOT_FOUND);
|
||||
return handle_error(http::status_codes::NOT_FOUND);
|
||||
}
|
||||
|
||||
return std::make_unique<http_response>(file_info->second, filename, status);
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
std::string file_extension = file_info->first.substr(file_info->first.rfind('.') + 1);
|
||||
std::string content_type = "text/html";
|
||||
|
||||
auto mime_type = http::mime_types.find(file_extension);
|
||||
if (mime_type != http::mime_types.end()) {
|
||||
content_type = mime_type->second;
|
||||
}
|
||||
|
||||
resp->add_body_ref(&file_info->second);
|
||||
resp->add_status(http::status_codes::OK);
|
||||
resp->add_header(http::header("Content-Type", content_type), false);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
void populate_cache_dir(std::string dir) {
|
||||
void file_backend::populate_cache_dir(std::string dir) {
|
||||
std::filesystem::recursive_directory_iterator cur = begin(std::filesystem::recursive_directory_iterator(dir));
|
||||
std::filesystem::recursive_directory_iterator fin = end(std::filesystem::recursive_directory_iterator(dir));
|
||||
|
||||
|
|
@ -31,37 +45,43 @@ private:
|
|||
std::ifstream stream(filename);
|
||||
buffer << stream.rdbuf();
|
||||
file_cache[filename] = buffer.str();
|
||||
std::cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << std::endl;
|
||||
log::verbose << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << std::endl;
|
||||
++cur;
|
||||
}
|
||||
}
|
||||
|
||||
void populate_cache() {
|
||||
void file_backend::populate_cache() {
|
||||
populate_cache_dir(file_dir);
|
||||
populate_cache_dir("./error_pages/");
|
||||
}
|
||||
|
||||
public:
|
||||
file_backend(std::string dir = "./www") : file_dir(std::move(dir)) {
|
||||
file_backend::file_backend(std::string dir) : file_dir(std::move(dir)) {
|
||||
populate_cache();
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<http_response> handle_request(http_request& req) override {
|
||||
std::unique_ptr<http::response> file_backend::handle_request(http::request& req) {
|
||||
return handle_request_cache(req);
|
||||
}
|
||||
|
||||
std::unique_ptr<http_response> handle_error(const http_status_codes& error) {
|
||||
std::unique_ptr<http::response> file_backend::handle_error(const http::status_codes& error) {
|
||||
std::string filename = "./error_pages/" + std::to_string(error) + ".html";
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
||||
http_status_codes status = error;
|
||||
http::status_codes status = error;
|
||||
if (file_info == file_cache.end()) {
|
||||
status = http_status_codes::NOT_FOUND;
|
||||
status = http::status_codes::NOT_FOUND;
|
||||
filename = "./error_pages/404.html";
|
||||
file_info = file_cache.find(filename);
|
||||
}
|
||||
|
||||
return std::make_unique<http_response>(file_info->second, filename, status);
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
resp->add_body_ref(&file_info->second);
|
||||
resp->add_status(error);
|
||||
resp->add_header(http::header("Content-Type", "text/html"), false);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
22
src/backends/file_backend.hpp
Normal file
22
src/backends/file_backend.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "backend.hpp"
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
class file_backend : public backend {
|
||||
private:
|
||||
std::unordered_map<std::string,std::string> file_cache;
|
||||
std::string file_dir;
|
||||
|
||||
std::unique_ptr<http::response> handle_request_cache(http::request& req);
|
||||
void populate_cache_dir(std::string dir);
|
||||
void populate_cache();
|
||||
public:
|
||||
file_backend(std::string dir = "./www") ;
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) override;
|
||||
std::unique_ptr<http::response> handle_error(const http::status_codes& error);
|
||||
};
|
||||
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue