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// clang-format off
16
17/// @ingroup userver_components userver_http_handlers
18///
19/// @brief Handler that returns HTTP 200 if file exist and returns file data with mapped content/type.
20///
21/// Path arguments of this handle are passed to `fs-cache-component` to get the file. In other words, for the following
22/// `fs-cache-main`:
23/// @code{.yaml}
24/// fs-cache-main:
25/// dir: /fs-cache-main-path/ # Path to the directory with files
26/// @endcode
27/// the `handler-static` with `path: /handler-static-path/*` on request to `/handler-static-path/some/file.html`
28/// would return file at path `/fs-cache-main-path/some/file.html`.
29///
30/// ## HttpHandlerStatic Dynamic config
31/// * @ref USERVER_FILES_CONTENT_TYPE_MAP
32///
33/// \ref userver_http_handlers "Userver HTTP Handlers".
34///
35/// ## Static options:
36/// Inherits all the options from server::handlers::HttpHandlerBase and adds the
37/// following ones:
38///
39/// Name | Description | Default value
40/// ------------------ | ----------------------------------------------------------------------------------------- | -------------
41/// fs-cache-component | Name of the components::FsCache component | fs-cache-component
42/// expires | Cache age in seconds | 600
43/// directory-file | File to return for directory requests. File name (not path) search in requested directory | "index.html"
44/// not-found-file | File to return for missing files | "/404.html"
45///
46/// ## Example usage:
47///
48/// @snippet samples/static_service/main.cpp Static service sample - main
49
50// clang-format on
51
52class HttpHandlerStatic final : public HttpHandlerBase {
53public:
54 /// @ingroup userver_component_names
55 /// @brief The default name of server::handlers::HttpHandlerStatic
56 static constexpr std::string_view kName = "handler-static";
57
58 using HttpHandlerBase::HttpHandlerBase;
59
60 HttpHandlerStatic(const components::ComponentConfig& config, const components::ComponentContext& context);
61
62 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const override;
63
64 static yaml_config::Schema GetStaticConfigSchema();
65
66private:
67 dynamic_config::Source config_;
68 const fs::FsCacheClient& storage_;
69 const std::chrono::seconds cache_age_;
70 const std::string directory_file_;
71 const std::string not_found_file_;
72};
73
74} // namespace server::handlers
75
76USERVER_NAMESPACE_END