Malloy
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
malloy
server
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
12
namespace
boost::asio::ssl
13
{
14
class
context;
15
}
16
17
namespace
malloy::server
18
{
19
20
class
router
;
21
class
listener
;
22
29
class
routing_context
30
{
31
public
:
32
using
session =
malloy::detail::controller_run_result<std::shared_ptr<malloy::server::listener>
>;
33
37
struct
config
:
38
malloy::controller::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
111
const
malloy::server::router
&
112
router
() const noexcept
113
{
114
return
m_router;
115
}
116
122
[[nodiscard]]
123
constexpr
124
malloy::server::router
&
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
164
return
session{ctrl.m_cfg, std::move(l), std::move(ioc)};
165
}
166
};
167
168
auto
start(
routing_context
&&) -> routing_context::session;
169
170
}
malloy::detail::controller_run_result
Definition
controller_run_result.hpp:43
malloy::server::listener
Accepts incoming connections.
Definition
listener.hpp:34
malloy::server::router
Definition
router.hpp:103
malloy::server::routing_context
A high-level controller.
Definition
routing_context.hpp:30
malloy::server::routing_context::init_tls
bool init_tls(const std::filesystem::path &cert_path, const std::filesystem::path &key_path)
Definition
routing_context.cpp:28
malloy::server::routing_context::router
constexpr const malloy::server::router & router() const noexcept
Definition
routing_context.hpp:112
malloy::server::routing_context::router
constexpr malloy::server::router & router() noexcept
Definition
routing_context.hpp:125
malloy::server::routing_context::config
Definition
routing_context.hpp:39
malloy::server::routing_context::config::doc_root
std::filesystem::path doc_root
Definition
routing_context.hpp:61
malloy::server::routing_context::config::port
std::uint16_t port
Definition
routing_context.hpp:53
malloy::server::routing_context::config::connection_logger
std::shared_ptr< spdlog::logger > connection_logger
Definition
routing_context.hpp:43
malloy::server::routing_context::config::agent_string
std::string agent_string
Agent string used for connections.
Definition
routing_context.hpp:67
Generated by
1.18.0