restructuring
This commit is contained in:
parent
ac669ba504
commit
6c9f7f7c49
13 changed files with 573 additions and 496 deletions
|
@ -1,13 +1,13 @@
|
|||
#include "backend.cpp"
|
||||
#include <filesystem>
|
||||
#include "backend.cpp"
|
||||
|
||||
class file_backend : public backend {
|
||||
private:
|
||||
unordered_map<string, string> file_cache;
|
||||
string file_dir;
|
||||
std::unordered_map<std::string,std::string> file_cache;
|
||||
std::string file_dir;
|
||||
|
||||
unique_ptr<http_response> handle_request_cache(http_request& req) {
|
||||
string filename = req.path() == "/" ? "/index.html" : req.path();
|
||||
std::unique_ptr<http_response> 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);
|
||||
|
||||
|
@ -18,21 +18,21 @@ private:
|
|||
file_info = file_cache.find(filename);
|
||||
}
|
||||
|
||||
return make_unique<http_response>(file_info->second, filename, status);
|
||||
return std::make_unique<http_response>(file_info->second, filename, status);
|
||||
}
|
||||
|
||||
void populate_cache_dir(string dir) {
|
||||
filesystem::recursive_directory_iterator cur = begin(filesystem::recursive_directory_iterator(dir));
|
||||
filesystem::recursive_directory_iterator fin = end(filesystem::recursive_directory_iterator(dir));
|
||||
void 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));
|
||||
|
||||
while (cur != fin) {
|
||||
auto p = cur->path();
|
||||
string filename = p.string();
|
||||
stringstream buffer;
|
||||
ifstream stream(filename);
|
||||
std::string filename = p.string();
|
||||
std::stringstream buffer;
|
||||
std::ifstream stream(filename);
|
||||
buffer << stream.rdbuf();
|
||||
file_cache[filename] = buffer.str();
|
||||
cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << endl;
|
||||
std::cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << std::endl;
|
||||
++cur;
|
||||
}
|
||||
}
|
||||
|
@ -40,14 +40,15 @@ private:
|
|||
void populate_cache() {
|
||||
populate_cache_dir(file_dir);
|
||||
populate_cache_dir("./error_pages/");
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
file_backend(string dir = "./www") : file_dir(dir) {
|
||||
file_backend(std::string dir = "./www") : file_dir(dir) {
|
||||
populate_cache();
|
||||
}
|
||||
|
||||
unique_ptr<http_response> handle_request(http_request& req) override {
|
||||
std::unique_ptr<http_response> handle_request(http_request& req) override {
|
||||
return handle_request_cache(req);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue