Malloy
Loading...
Searching...
No Matches
file.hpp
1#pragma once
2
3#include "../response.hpp"
4#include "../request.hpp"
5#include "../../error.hpp"
6
7#include <filesystem>
8#include <variant>
9
17{
18
23 template<bool isRequest>
25 {
28 using value_type = boost::beast::http::file_body::value_type;
29 using header_type = boost::beast::http::header<isRequest>;
30 using setup_handler_t = std::function<void(const header_type&, value_type&)>;
31
35 setup_handler_t setup;
36
42 basic_file() = default;
43
47 explicit
48 basic_file(setup_handler_t setup_) :
49 setup{ std::move(setup_) }
50 {
51 }
52
56 basic_file(basic_file&&) noexcept = default;
57
64 operator=(basic_file&&) noexcept = default;
65
72 [[nodiscard]]
73 static
76 const std::filesystem::path& location,
77 std::function<void(malloy::error_code)> on_error,
78 boost::beast::file_mode mode
79 )
80 {
81 return basic_file{ [location, mode, on_error = std::move(on_error)](auto&&, auto& body) {
82 boost::beast::error_code ec;
83 body.open(location.string().c_str(), mode, ec);
84 if (ec && on_error)
85 on_error(ec);
86 } };
87 }
88
89 [[nodiscard]]
90 std::variant<boost::beast::http::file_body>
91 body_for(const header_type&) const
92 {
93 return { };
94 }
95
96 void
97 setup_body(const header_type& h, value_type& body) const
98 {
99 if (setup)
100 setup(h, body);
101 }
102 };
103
104 using file_request = basic_file<true>;
105 using file_response = basic_file<false>;
106
107}
Definition: request.hpp:19
Definition: response.hpp:22
Contains the filter types bundled with malloy.
Definition: controller.hpp:32
boost::beast::error_code error_code
Error code used to signify errors without throwing. Truthy means it holds an error.
Definition: error.hpp:9
Writes the contents of a message to disk.
Definition: file.hpp:25
basic_file(setup_handler_t setup_)
Construct with a setup handler.
Definition: file.hpp:48
static basic_file open(const std::filesystem::path &location, std::function< void(malloy::error_code)> on_error, boost::beast::file_mode mode)
Create a version of the filter that writes/reads the specified file.
Definition: file.hpp:75
basic_file(basic_file &&) noexcept=default
basic_file()=default
Default ctor.
setup_handler_t setup
Definition: file.hpp:35