Implemented caching for index.html
All checks were successful
/ Build the server (push) Successful in 2m52s

This commit is contained in:
Mutzi 2023-10-23 17:33:19 +02:00
parent 21b4a8800e
commit 13579acd8f
Signed by: root
GPG Key ID: 2437494E09F13876
2 changed files with 33 additions and 10 deletions

View File

@ -2,7 +2,7 @@ add_subdirectory(spdlog-1.12.0)
add_subdirectory(restbed-4.8) add_subdirectory(restbed-4.8)
add_custom_command( add_custom_command(
COMMAND ./configure.py ARGS --amalgamation --disable-shared --minimized-build --enable-modules=argon2fmt,hotp,base32,auto_rng,system_rng,tls13,certstor_system,certstor_flatfile COMMAND ./configure.py ARGS --amalgamation --disable-shared --minimized-build --enable-modules=argon2fmt,hotp,base32,auto_rng,system_rng,tls13,certstor_system,certstor_flatfile,md5
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.h OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.h
) )

View File

@ -1,5 +1,7 @@
#include <memory> #include <memory>
#include <csignal> #include <csignal>
#include <botan_all.h>
#include <corvusoft/restbed/request.hpp>
#include <corvusoft/restbed/resource.hpp> #include <corvusoft/restbed/resource.hpp>
#include <corvusoft/restbed/session.hpp> #include <corvusoft/restbed/session.hpp>
#include <corvusoft/restbed/settings.hpp> #include <corvusoft/restbed/settings.hpp>
@ -26,6 +28,13 @@ int main() {
spdlog::default_logger()->sinks().push_back(file_sink); spdlog::default_logger()->sinks().push_back(file_sink);
spdlog::set_level(spdlog::level::trace); 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>(); auto mrpc_resource = std::make_shared<restbed::Resource>();
mrpc_resource->set_path("/mrpc"); mrpc_resource->set_path("/mrpc");
Server server{mrpc_resource}; Server server{mrpc_resource};
@ -41,15 +50,29 @@ int main() {
auto index_resource = std::make_shared<restbed::Resource>(); auto index_resource = std::make_shared<restbed::Resource>();
index_resource->set_path("/"); index_resource->set_path("/");
index_resource->set_method_handler("GET", [](const std::shared_ptr<restbed::Session>& s){ index_resource->set_method_handler("GET", [&index_etag](const std::shared_ptr<restbed::Session>& s){
s->yield( auto req = s->get_request();
200, if (req->get_header("If-None-Match", "") == index_etag) {
index_html_bytes, s->yield(
std::multimap<std::string, std::string>{ 304,
{"Content-Type", "text/html"}, "",
{"Content-Length", std::to_string(index_html_len)} 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>(); auto favicon_resource = std::make_shared<restbed::Resource>();