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:
parent
21b4a8800e
commit
13579acd8f
@ -2,7 +2,7 @@ add_subdirectory(spdlog-1.12.0)
|
||||
add_subdirectory(restbed-4.8)
|
||||
|
||||
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
|
||||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Botan-3.2.0/botan_all.h
|
||||
)
|
||||
|
27
src/main.cxx
27
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){
|
||||
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)}
|
||||
{"Content-Length", std::to_string(index_html_len)},
|
||||
{"Cache-Control", "no-cache"},
|
||||
{"ETag", index_etag}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
auto favicon_resource = std::make_shared<restbed::Resource>();
|
||||
|
Loading…
Reference in New Issue
Block a user