Malloy
Loading...
Searching...
No Matches
endpoint_http_files.hpp
1#pragma once
2
3#include "endpoint_http.hpp"
4#include "../../core/http/request.hpp"
5#include "../../core/http/utils.hpp"
6#include "../../core/type_traits.hpp"
7
8#include <filesystem>
9
10namespace malloy::server
11{
21 public endpoint_http
22 {
23 using write_func = writer_t<boost::beast::http::file_body, boost::beast::http::string_body>;
24
25 public:
26 std::string resource_base;
27 std::filesystem::path base_path;
28 std::string cache_control;
29
30 write_func writer;
31
32 [[nodiscard]]
33 bool matches(const req_header_t& head) const override
34 {
35 return malloy::http::resource_string(head).starts_with(resource_base);
36 }
37
38 [[nodiscard]]
39 handle_retr handle(const req_t& req, const http::connection_t& conn) const override
40 {
41 std::visit([this, conn](auto& gen) {
42 gen->template body<boost::beast::http::string_body>([this, conn](auto&& req) {
43 malloy::http::request<> req_clone{ req };
44 malloy::http::chop_resource(req_clone, resource_base);
45
46 // Create response
47 auto resp = malloy::http::generator::file(req_clone, base_path);
48 std::visit([this]<typename Resp>(Resp& resp){ resp.set(malloy::http::field::cache_control, cache_control); }, resp); // Add Cache-Control header
49
50 // Send
51 writer(req, std::move(resp), conn);
52 });
53 },
54 req
55 );
56
57 return std::nullopt;
58 }
59 };
60
61}
static file_response file(const request< Body > &req, const std::filesystem::path &storage_base_path)
Definition: generator.hpp:114
Definition: request.hpp:19
Endpoint for file serving.
Definition: endpoint_http_files.hpp:22
bool matches(const req_header_t &head) const override
Definition: endpoint_http_files.hpp:33
handle_retr handle(const req_t &req, const http::connection_t &conn) const override
Definition: endpoint_http_files.hpp:39
Definition: endpoint_http.hpp:22