userver: userver/server/handlers/http_handler_static.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
http_handler_static.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/http_handler_static.hpp
4/// @brief @copybrief server::handlers::HttpHandlerStatic
5
6#include <userver/components/fs_cache.hpp>
7#include <userver/dynamic_config/source.hpp>
8#include <userver/fs/fs_cache_client.hpp>
9#include <userver/server/handlers/http_handler_base.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace server::handlers {
14
15// clang-format off
16
17/// @ingroup userver_components userver_http_handlers
18///
19/// @brief Handler that returns HTTP 200 if file exist
20/// and returns file data with mapped content/type
21///
22/// ## HttpHandlerStatic Dynamic config
23/// * @ref USERVER_FILES_CONTENT_TYPE_MAP
24///
25/// \ref userver_http_handlers "Userver HTTP Handlers".
26///
27/// ## Static options:
28/// Inherits all the options from server::handlers::HttpHandlerBase and adds the
29/// following ones:
30///
31/// Name | Description | Default value
32/// ------------------ | ----------------------------- | -------------
33/// fs-cache-component | Name of the FsCache component | fs-cache-component
34/// expires | Cache age in seconds | 600
35///
36/// ## Example usage:
37///
38/// @snippet samples/static_service/static_service.cpp Static service sample - main
39
40// clang-format on
41
42class HttpHandlerStatic final : public HttpHandlerBase {
43public:
44 /// @ingroup userver_component_names
45 /// @brief The default name of server::handlers::HttpHandlerStatic
46 static constexpr std::string_view kName = "handler-static";
47
48 using HttpHandlerBase::HttpHandlerBase;
49
50 HttpHandlerStatic(const components::ComponentConfig& config, const components::ComponentContext& context);
51
52 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const override;
53
54 static yaml_config::Schema GetStaticConfigSchema();
55
56private:
57 dynamic_config::Source config_;
58 const fs::FsCacheClient& storage_;
59 const std::chrono::seconds cache_age_;
60};
61
62} // namespace server::handlers
63
64USERVER_NAMESPACE_END