performance improvements + a good amount of changes
This commit is contained in:
		
							parent
							
								
									89a6d9a528
								
							
						
					
					
						commit
						0649a28a28
					
				
					 24 changed files with 2040744 additions and 70 deletions
				
			
		
							
								
								
									
										72
									
								
								src/backends/file_backend.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/backends/file_backend.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,72 @@
 | 
			
		|||
#include "backend.cpp"
 | 
			
		||||
#include <filesystem>
 | 
			
		||||
 | 
			
		||||
class file_backend : public backend {
 | 
			
		||||
private:
 | 
			
		||||
  unordered_map<string, string> file_cache;
 | 
			
		||||
  bool cache_enabled;
 | 
			
		||||
 | 
			
		||||
  unique_ptr<http_response> handle_request_nocache(http_request& req) {
 | 
			
		||||
     string filename = req.path() == "/" ? "index.html" : req.path();
 | 
			
		||||
     filename = "./www/" + filename;
 | 
			
		||||
     ifstream stream(filename);
 | 
			
		||||
 | 
			
		||||
     int status = 200;
 | 
			
		||||
     if (!stream.is_open()) {
 | 
			
		||||
         status = 404;
 | 
			
		||||
         filename = "./error_pages/404.html";
 | 
			
		||||
         stream = ifstream(filename);
 | 
			
		||||
     }
 | 
			
		||||
 | 
			
		||||
     stringstream buffer;
 | 
			
		||||
     buffer << stream.rdbuf();
 | 
			
		||||
     return make_unique<http_response>(buffer.str(), status);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  unique_ptr<http_response> handle_request_cache(http_request& req) {
 | 
			
		||||
     string filename = req.path() == "/" ? "/index.html" : req.path();
 | 
			
		||||
     filename = "./www" + filename;
 | 
			
		||||
     auto file_info = file_cache.find(filename);
 | 
			
		||||
 | 
			
		||||
     int status = 200;
 | 
			
		||||
     if (file_info == file_cache.end()) {
 | 
			
		||||
         status = 404;
 | 
			
		||||
         filename = "./error_pages/404.html";
 | 
			
		||||
         file_info = file_cache.find(filename);
 | 
			
		||||
     }
 | 
			
		||||
 | 
			
		||||
     return make_unique<http_response>(file_info->second, 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));
 | 
			
		||||
 | 
			
		||||
    while (cur != fin) {
 | 
			
		||||
        auto p = cur->path();
 | 
			
		||||
        string filename = p.string();
 | 
			
		||||
        stringstream buffer;
 | 
			
		||||
        ifstream stream(filename);
 | 
			
		||||
        buffer << stream.rdbuf();
 | 
			
		||||
        file_cache[filename] = buffer.str();
 | 
			
		||||
        cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << endl;
 | 
			
		||||
        ++cur;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void populate_cache() {
 | 
			
		||||
    populate_cache_dir("./www/");
 | 
			
		||||
    populate_cache_dir("./error_pages/");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
  file_backend(bool enable_cache) : cache_enabled(enable_cache) {
 | 
			
		||||
    if(cache_enabled) {
 | 
			
		||||
      populate_cache(); 
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  unique_ptr<http_response> handle_request(http_request& req) override {
 | 
			
		||||
    return cache_enabled ? handle_request_cache(req) : handle_request_nocache(req);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue