Skip to content

Commit

Permalink
refactor how server header is populated
Browse files Browse the repository at this point in the history
now it generated in one place

little fancy on the cmake with private features, requires C++ 17 compiler, conanfile needs conan-io/conan#8002
  • Loading branch information
prince-chrismc committed Nov 29, 2020
1 parent c66f226 commit 9cd8a97
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 40 deletions.
6 changes: 3 additions & 3 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.8) # using cxx_std_17

project(user-management)

Expand Down Expand Up @@ -47,10 +47,10 @@ if(${RUN_TIDY})
setup_cmake_format(main "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt")
endif()

if(${BUILD_WEB_SERVER})
if(BUILD_WEB_SERVER)
add_subdirectory(src)
endif()

if(${BUILD_TESTS})
if(BUILD_TESTS)
add_subdirectory(test)
endif()
14 changes: 11 additions & 3 deletions backend/conan.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"requires": [
"1",
"10",
"12"
"12",
"13"
],
"build_requires": [
"13"
"14"
],
"path": "conanfile.py",
"context": "host"
},
"1": {
"ref": "restinio/0.6.12#5e99922ca31945e41e2c3d4cb8998720",
"ref": "restinio/0.6.12#f9314f8afdfe86de98f6fac66020c517",
"requires": [
"2",
"3",
Expand Down Expand Up @@ -76,6 +77,13 @@
"context": "host"
},
"13": {
"ref": "spdlog/1.8.1#bc83c6710733bf9bee8c664389158565",
"requires": [
"3"
],
"context": "host"
},
"14": {
"ref": "catch2/2.13.3#a4f1ed8fa355ba5a76c422fdfe94724d",
"context": "host"
}
Expand Down
1 change: 1 addition & 0 deletions backend/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def requirements(self):
self.requires("restinio/0.6.12")
self.requires("json-schema-validator/2.1.0")
self.requires("lyra/1.5.0")
self.requires("spdlog/1.8.1")

def build(self):
cmake = CMake(self)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_subdirectory("handlers")
add_subdirectory("utility")

add_executable(user_database_app "main.cpp")
target_link_libraries(user_database_app user_routes serve_files web_app app_args server_logger)
target_link_libraries(user_database_app PRIVATE user_routes serve_files web_app app_args server_logger)

if(${RUN_TIDY})
include("clang-tidy")
Expand Down
4 changes: 2 additions & 2 deletions backend/src/handlers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ target_link_libraries(user_routes PUBLIC routing users_databse response_builder)
add_library(serve_files "serve_files.hpp" "serve_files.cpp")
target_include_directories(serve_files PUBLIC "..")
set_target_properties(serve_files PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(serve_files PUBLIC content_type routing)
target_link_libraries(serve_files PUBLIC routing PRIVATE content_type add_headers)

add_library(web_app "web_app.hpp" "web_app.cpp")
target_include_directories(web_app PUBLIC "..")
set_target_properties(web_app PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(web_app PUBLIC content_type routing)
target_link_libraries(web_app PUBLIC routing PRIVATE content_type add_headers)

if(${RUN_TIDY})
include("clang-tidy")
Expand Down
14 changes: 4 additions & 10 deletions backend/src/handlers/serve_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "serve_files.hpp"

#include "utility/add_headers.hpp"
#include "utility/content_type_from_ext.hpp"

namespace handler {
Expand All @@ -18,28 +19,21 @@ request_status from_disk::operator()(const request_handle& req, route_params /*p
try {
auto sf = restinio::sendfile(file_path);
auto modified_at = restinio::make_date_field_value(sf.meta().last_modified_at());

auto expires_at = restinio::make_date_field_value(std::chrono::system_clock::now() + std::chrono::hours(24 * 7));

return req->create_response()
.append_header(restinio::http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
.append_header_date_field()
return response::impl::add_generic_headers(req->create_response())
.append_header(restinio::http_field::last_modified, std::move(modified_at))
.append_header(restinio::http_field::expires, std::move(expires_at))
.append_header(restinio::http_field::content_type, content_type_by_file_extention(ext))
.set_body(std::move(sf))
.done();
} catch (const std::exception&) {
return req->create_response(restinio::status_not_found())
.append_header(restinio::http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
.append_header_date_field()
return response::impl::add_generic_headers(req->create_response(restinio::status_not_found()))
.connection_close()
.done();
}
} else {
return req->create_response(restinio::status_forbidden())
.append_header(restinio::http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
.append_header_date_field()
return response::impl::add_generic_headers(req->create_response(restinio::status_forbidden()))
.connection_close()
.done();
}
Expand Down
10 changes: 9 additions & 1 deletion backend/src/handlers/utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ target_include_directories(routing PUBLIC "..")
set_target_properties(routing PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(routing PUBLIC restinio::restinio nonstd::string-view-lite) # Temporary work

add_library(add_headers "add_headers.hpp" "add_headers.cpp")
target_include_directories(add_headers PUBLIC "..")
set_target_properties(add_headers PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(add_headers PUBLIC restinio::restinio PRIVATE user_management fmt::fmt)
target_compile_features(add_headers PRIVATE cxx_std_17)

add_library(response_builder "response_builder.hpp" "response_builder.cpp")
target_include_directories(response_builder PUBLIC "..")
set_target_properties(response_builder PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(response_builder PUBLIC routing user_management)
target_link_libraries(response_builder PUBLIC routing add_headers user_management)

add_library(content_type "content_type_from_ext.hpp" "content_type_from_ext.cpp")
target_include_directories(content_type PUBLIC "..")
Expand All @@ -18,11 +24,13 @@ target_link_libraries(content_type PUBLIC nonstd::string-view-lite)
if(${RUN_TIDY})
include("clang-tidy")
setup_clang_tidy(routing)
setup_clang_tidy(add_headers)
setup_clang_tidy(response_builder)
setup_clang_tidy(content_type)

include("clang-format")
setup_clang_format(routing)
setup_clang_format(add_headers)
setup_clang_format(response_builder)
setup_clang_format(content_type)

Expand Down
19 changes: 19 additions & 0 deletions backend/src/handlers/utility/add_headers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// MIT License

#include "add_headers.hpp"

#include <fmt/compile.h>
#include <restinio/version.hpp>

#include "um/user_management.hpp"

namespace handler {
namespace response {
namespace impl {
std::string server_declaration() {
return fmt::format(FMT_COMPILE("user-management/{}; restinio/{}.{}.{}"), user_management::version,
RESTINIO_VERSION_MAJOR, RESTINIO_VERSION_MINOR, RESTINIO_VERSION_PATCH);
}
} // namespace impl
} // namespace response
} // namespace handler
26 changes: 26 additions & 0 deletions backend/src/handlers/utility/add_headers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// MIT License

#ifndef HANDLERS_UTILITY_ADD_HEADERS_HPP_
#define HANDLERS_UTILITY_ADD_HEADERS_HPP_

#include <restinio/http_headers.hpp>

#include <chrono>
#include <utility>

namespace handler {
namespace response {
namespace impl {
std::string server_declaration();

template <class response_builder>
response_builder add_generic_headers(response_builder builder) {
builder.append_header(restinio::http_field::server, server_declaration())
.append_header_date_field();
return std::move(builder);
}
} // namespace impl
} // namespace response
} // namespace handler

#endif // HANDLERS_UTILITY_ADD_HEADERS_HPP_
12 changes: 8 additions & 4 deletions backend/src/handlers/utility/response_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

#include "response_builder.hpp"

#include "add_headers.hpp"

namespace handler {
namespace response {
namespace impl {
void add_generic_headers(builder& builder) {
builder.append_header(http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
builder.append_header(http_field::server, server_declaration())
.append_header(http_field::date, std::chrono::system_clock::now());
}
void add_cors_headers(builder& builder) {
builder.append_header(http_field::access_control_allow_origin, "*")
.append_header(restinio::http_field::access_control_allow_headers, "Content-Type")
.append_header(restinio::http_field::access_control_max_age, "86400");
.append_header(http_field::access_control_allow_headers, "Content-Type")
.append_header(http_field::access_control_max_age, "86400");
}
void add_api_headers(builder& builder) {
builder.append_header(http_field::content_type, "application/json");
}
void add_api_headers(builder& builder) { builder.append_header(http_field::content_type, "application/json"); }
} // namespace impl

builder::builder(const request_handle& req, const http_status_line& status) : builder_{req->create_response(status)} {
Expand Down
7 changes: 0 additions & 7 deletions backend/src/handlers/utility/response_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ namespace response {
using http_field = restinio::http_field;
using http_status_line = restinio::http_status_line_t;

class builder;
namespace impl {
void add_generic_headers(builder& builder);
void add_cors_headers(builder& builder);
void add_api_headers(builder& builder);
} // namespace impl

class builder {
restinio::response_builder_t<restinio::restinio_controlled_output_t> builder_;

Expand Down
9 changes: 3 additions & 6 deletions backend/src/handlers/web_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#include "web_app.hpp"

#include "utility/content_type_from_ext.hpp"
#include "utility/add_headers.hpp"

namespace handler {
namespace web_app {
request_status link(const request_handle& req, route_params /*params*/) {
static auto modified_at = restinio::make_date_field_value(std::chrono::system_clock::now());
auto expires_at = restinio::make_date_field_value(std::chrono::system_clock::now() + std::chrono::hours(24 * 7));

return req->create_response()
.append_header_date_field()
.append_header(restinio::http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
return response::impl::add_generic_headers(req->create_response())
.append_header(restinio::http_field::last_modified, std::move(modified_at))
.append_header(restinio::http_field::expires, std::move(expires_at))
.append_header(restinio::http_field::content_type, content_type_by_file_extention("html"))
Expand All @@ -22,9 +21,7 @@ request_status link(const request_handle& req, route_params /*params*/) {
}

request_status redirect(const request_handle& req, route_params /*params*/) {
return req->create_response(restinio::status_found())
.append_header_date_field()
.append_header(restinio::http_field::server, "user-management/1.0.0-dev.0; restinio/0.6.12")
return response::impl::add_generic_headers(req->create_response(restinio::status_found()))
.append_header(restinio::http_field::location, "/index.html")
.done();
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/utility/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
find_package(lyra 1.5.0 REQUIRED)
find_package(fmt 7.0.3 REQUIRED)
find_package(fmt 7.1.2 REQUIRED)
find_package(expected-lite 0.4.0 REQUIRED)
find_package(spdlog 1.8.0 REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions backend/src/utility/server_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#ifndef UTILITY_SERVER_LOGGER
#define UTILITY_SERVER_LOGGER

#include "spdlog/sinks/syslog_sink.h"
#include "spdlog/spdlog.h"
#include <spdlog/sinks/syslog_sink.h>
#include <spdlog/spdlog.h>

struct server_logger {
server_logger() { spdlog::syslog_logger_mt("server", "um_app_server", LOG_PID)->set_level(spdlog::level::info); }
Expand Down

0 comments on commit 9cd8a97

Please sign in to comment.