Implemented caching for index.html
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				/ Build the server (push) Successful in 2m52s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	/ Build the server (push) Successful in 2m52s
				
			This commit is contained in:
		
							
								
								
									
										41
									
								
								src/main.cxx
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/main.cxx
									
									
									
									
									
								
							@@ -1,5 +1,7 @@
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <csignal>
 | 
			
		||||
#include <botan_all.h>
 | 
			
		||||
#include <corvusoft/restbed/request.hpp>
 | 
			
		||||
#include <corvusoft/restbed/resource.hpp>
 | 
			
		||||
#include <corvusoft/restbed/session.hpp>
 | 
			
		||||
#include <corvusoft/restbed/settings.hpp>
 | 
			
		||||
@@ -26,6 +28,13 @@ int main() {
 | 
			
		||||
    spdlog::default_logger()->sinks().push_back(file_sink);
 | 
			
		||||
    spdlog::set_level(spdlog::level::trace);
 | 
			
		||||
 | 
			
		||||
    std::string index_etag;
 | 
			
		||||
    {
 | 
			
		||||
        auto md5_hash = Botan::HashFunction::create_or_throw("MD5");
 | 
			
		||||
        md5_hash->update(index_html_bytes);
 | 
			
		||||
        index_etag = Botan::hex_encode(md5_hash->final());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto mrpc_resource = std::make_shared<restbed::Resource>();
 | 
			
		||||
    mrpc_resource->set_path("/mrpc");
 | 
			
		||||
    Server server{mrpc_resource};
 | 
			
		||||
@@ -41,15 +50,29 @@ int main() {
 | 
			
		||||
 | 
			
		||||
    auto index_resource = std::make_shared<restbed::Resource>();
 | 
			
		||||
    index_resource->set_path("/");
 | 
			
		||||
    index_resource->set_method_handler("GET", [](const std::shared_ptr<restbed::Session>& s){
 | 
			
		||||
        s->yield(
 | 
			
		||||
            200,
 | 
			
		||||
            index_html_bytes,
 | 
			
		||||
            std::multimap<std::string, std::string>{
 | 
			
		||||
                {"Content-Type", "text/html"},
 | 
			
		||||
                {"Content-Length", std::to_string(index_html_len)}
 | 
			
		||||
            }
 | 
			
		||||
        );
 | 
			
		||||
    index_resource->set_method_handler("GET", [&index_etag](const std::shared_ptr<restbed::Session>& s){
 | 
			
		||||
        auto req = s->get_request();
 | 
			
		||||
        if (req->get_header("If-None-Match", "") == index_etag) {
 | 
			
		||||
            s->yield(
 | 
			
		||||
                304,
 | 
			
		||||
                "",
 | 
			
		||||
                std::multimap<std::string, std::string>{
 | 
			
		||||
                    {"Cache-Control", "no-cache"},
 | 
			
		||||
                    {"ETag", index_etag}
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        } else {
 | 
			
		||||
            s->yield(
 | 
			
		||||
                200,
 | 
			
		||||
                index_html_bytes,
 | 
			
		||||
                std::multimap<std::string, std::string>{
 | 
			
		||||
                    {"Content-Type",   "text/html"},
 | 
			
		||||
                    {"Content-Length", std::to_string(index_html_len)},
 | 
			
		||||
                    {"Cache-Control",  "no-cache"},
 | 
			
		||||
                    {"ETag",           index_etag}
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    auto favicon_resource = std::make_shared<restbed::Resource>();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user