userver: userver/server/handlers/http_handler_static.hpp Source File
Loading...
Searching...
No Matches
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/// @ingroup userver_components userver_http_handlers
16///
17/// @brief Handler that returns HTTP 200 if file exist and returns file data with mapped content/type.
18///
19/// Path arguments of this handle are passed to `fs-cache-component` to get the file. In other words, for the following
20/// `fs-cache-main`:
21/// @code{.yaml}
22/// fs-cache-main:
23/// dir: /fs-cache-main-path/ # Path to the directory with files
24/// @endcode
25/// the `handler-static` with `path: /handler-static-path/*` on request to `/handler-static-path/some/file.html`
26/// would return file at path `/fs-cache-main-path/some/file.html`.
27///
28/// ## HttpHandlerStatic Dynamic config
29/// * @ref USERVER_FILES_CONTENT_TYPE_MAP
30///
31/// \ref userver_http_handlers "Userver HTTP Handlers".
32///
33/// ## Static options of server::handlers::HttpHandlerStatic :
34/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/http_handler_static.md
35///
36/// Options inherited from @ref server::handlers::HttpHandlerBase :
37/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/http_handler_base.md
38///
39/// Options inherited from @ref server::handlers::HandlerBase :
40/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/handler_base.md
41///
42/// Options inherited from @ref components::ComponentBase :
43/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
44///
45/// ## Example usage:
46///
47/// @snippet samples/static_service/main.cpp Static service sample - main
48class HttpHandlerStatic final : public HttpHandlerBase {
49public:
50 /// @ingroup userver_component_names
51 /// @brief The default name of server::handlers::HttpHandlerStatic
52 static constexpr std::string_view kName = "handler-static";
53
54 using HttpHandlerBase::HttpHandlerBase;
55
56 HttpHandlerStatic(const components::ComponentConfig& config, const components::ComponentContext& context);
57
58 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const override;
59
60 static yaml_config::Schema GetStaticConfigSchema();
61
62private:
63 dynamic_config::Source config_;
64 const fs::FsCacheClient& storage_;
65 const std::chrono::seconds cache_age_;
66 const std::string directory_file_;
67 const std::string not_found_file_;
68};
69
70} // namespace server::handlers
71
72USERVER_NAMESPACE_END