Malloy
Loading...
Searching...
No Matches
routing_context.hpp
1#pragma once
2
3#include "../core/controller.hpp"
4#include "../core/detail/controller_run_result.hpp"
5#include "../server/listener.hpp"
6#include "../server/routing/router.hpp"
7
8#include <memory>
9#include <filesystem>
10#include <string>
11
12namespace boost::asio::ssl
13{
14 class context;
15}
16
17namespace malloy::server
18{
19
20 class router;
21 class listener;
22
30 {
31 public:
33
37 struct config :
39 {
43 std::shared_ptr<spdlog::logger> connection_logger;
44
48 std::string interface = "127.0.0.1";
49
53 std::uint16_t port = 8080;
54
61 std::filesystem::path doc_root = ".";
62
67 std::string agent_string{"malloy"};
68 };
69
70 explicit routing_context(config cfg);
71 routing_context(const routing_context& other) = delete;
72 routing_context(routing_context&& other) noexcept = default;
73 ~routing_context() = default;
74
75 routing_context& operator=(const routing_context& rhs) = delete;
76 routing_context& operator=(routing_context&& rhs) noexcept = default;
77
78 #if MALLOY_FEATURE_TLS
88 bool
89 init_tls(const std::filesystem::path& cert_path, const std::filesystem::path& key_path);
90
100 bool
101 init_tls(const std::string& cert, const std::string& key);
102 #endif
103
109 [[nodiscard]]
110 constexpr
112 router() const noexcept
113 {
114 return m_router;
115 }
116
122 [[nodiscard]]
123 constexpr
125 router() noexcept
126 {
127 return m_router;
128 }
129
130 private:
131 config m_cfg;
132 malloy::server::router m_router;
133 #if MALLOY_FEATURE_TLS
134 std::unique_ptr<boost::asio::ssl::context> m_tls_ctx;
135 #endif
136
137 [[nodiscard("ignoring result will cause the server to instantly stop")]]
138 friend
139 session
140 start(routing_context&& ctrl)
141 {
142 // Log
143 ctrl.m_cfg.logger->debug("starting server.");
144 auto ioc = std::make_unique<boost::asio::io_context>();
145
146 // Create the listener
147 auto l = std::make_shared<malloy::server::listener>(
148 ctrl.m_cfg.logger->clone("listener"),
149 ctrl.m_cfg.connection_logger,
150 *ioc,
151#if MALLOY_FEATURE_TLS
152 std::move(ctrl.m_tls_ctx),
153#else
154 nullptr,
155#endif
156 boost::asio::ip::tcp::endpoint{boost::asio::ip::make_address(ctrl.m_cfg.interface), ctrl.m_cfg.port},
157 std::make_shared<class router>(std::move(ctrl.m_router)),
158 std::make_shared<std::filesystem::path>(ctrl.m_cfg.doc_root),
159 ctrl.m_cfg.agent_string);
160
161 // Run the listener
162 l->run();
163 return session{ctrl.m_cfg, std::move(l), std::move(ioc)};
164 }
165 };
166
167 auto start(routing_context&&) -> routing_context::session;
168
169}
Definition: controller_run_result.hpp:42
Definition: router.hpp:104
A high-level controller.
Definition: routing_context.hpp:30
bool init_tls(const std::filesystem::path &cert_path, const std::filesystem::path &key_path)
Definition: routing_context.cpp:28
constexpr const malloy::server::router & router() const noexcept
Definition: routing_context.hpp:112
constexpr malloy::server::router & router() noexcept
Definition: routing_context.hpp:125
Definition: controller_run_result.hpp:19
Definition: routing_context.hpp:39
std::filesystem::path doc_root
Definition: routing_context.hpp:61
std::uint16_t port
Definition: routing_context.hpp:53
std::shared_ptr< spdlog::logger > connection_logger
Definition: routing_context.hpp:43
std::string agent_string
Agent string used for connections.
Definition: routing_context.hpp:67