17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SERVER 18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SERVER 27 #include <boost/asio/signal_set.hpp> 29 #include <condition_variable> 34 #pragma clang diagnostic push 35 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" 41 namespace Network::Http {
82 , std::shared_ptr<System::Clock>,
"" 83 , std::shared_ptr<EnvironmentProperties>,
"http.server" 84 ,
bool,
"http.server.register.signal.handler" 108 public:
HttpServer(std::shared_ptr<System::Clock> clock,
109 const std::shared_ptr<EnvironmentProperties>& configuration,
110 bool registerSignalHandler =
true);
130 public:
HttpServer(std::shared_ptr<System::Clock> clock,
131 const std::string & serverIdentification,
132 const TCP::endpoint & endpoint,
133 std::string threadNamePrefix_,
135 std::shared_ptr<HttpWebApp> httpHandler,
136 std::shared_ptr<WsWebApp> wsHandler = std::shared_ptr<WsWebApp>(
nullptr),
137 const std::string & loggingNamespace =
"balau.network.server",
138 std::string sessionCookieName =
"session",
140 bool registerSignalHandler =
true);
159 public:
HttpServer(std::shared_ptr<System::Clock> clock,
160 const std::string & serverIdentification,
161 const TCP::endpoint & endpoint,
162 std::string threadNamePrefix_,
165 const std::string & defaultFile =
"index.html",
166 const std::string & loggingNamespace =
"balau.network.server",
167 std::string sessionCookieName =
"session",
169 bool registerSignalHandler =
true);
188 public:
void startAsync();
198 public:
void startSync();
205 public:
bool isRunning();
214 public:
void stop(
bool warn =
false);
220 return toString(state->endpoint.address());
227 return state->endpoint.port();
234 : state(std::move(rhs.state))
235 , threadNamePrefix(rhs.threadNamePrefix)
236 , workerCount(rhs.workerCount)
237 , workers(std::move(rhs.workers))
238 , launched(std::move(rhs.launched))
239 , listener(std::move(rhs.listener))
240 , ioContext(std::move(rhs.ioContext))
241 , mutex(std::move(rhs.mutex))
242 , signalSet(std::move(rhs.signalSet)) {}
247 private:
static std::shared_ptr<HttpServerConfiguration> createState(std::shared_ptr<System::Clock> clock,
248 const std::shared_ptr<EnvironmentProperties> & configuration);
254 private:
static std::shared_ptr<MimeTypes> createMimeTypes(
const std::shared_ptr<EnvironmentProperties> & configuration,
255 BalauLogger & logger);
261 private:
static std::shared_ptr<HttpWebApp> createHttpHandler(
const std::shared_ptr<EnvironmentProperties> & configuration,
262 BalauLogger & logger);
268 private:
static std::shared_ptr<WsWebApp> createWsHandler(
const std::shared_ptr<EnvironmentProperties> & configuration,
269 BalauLogger & logger);
279 const std::string & locationStr,
280 std::shared_ptr<HttpWebApp> & webApp);
282 private:
void launchListener();
283 private:
void workerThreadFunction(
size_t workerIndex);
284 private:
void doRegisterSignalHandler();
285 private:
void handleSignal(
const boost::system::error_code & error,
int sig);
287 private: std::shared_ptr<HttpServerConfiguration> state;
288 private:
const std::string threadNamePrefix;
289 private:
const size_t workerCount;
290 private: std::vector<std::thread> workers;
291 private: std::unique_ptr<std::atomic_uint> launched;
292 private: std::shared_ptr<Impl::Listener> listener;
293 private: std::unique_ptr<boost::asio::io_context> ioContext;
294 private: std::unique_ptr<std::mutex> mutex;
295 private: std::unique_ptr<boost::asio::signal_set> signalSet;
302 #pragma clang diagnostic pop 304 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SERVER A file on the local file system.
Injection macros used in injectable classes.
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
An asynchronous HTTP/WebSocket server.
Definition: HttpServer.hpp:79
static std::shared_ptr< MimeTypes > defaultMimeTypes
The default set of mime types available.
Definition: MimeTypes.hpp:43
A file on the local file system.
Definition: File.hpp:35
An HTTP web application handler that routes to other handlers.
unsigned short getPort() const
Get the port being listened on.
Definition: HttpServer.hpp:226
Shared state between HTTP sessions.
#define BalauInjectNamedTypes(...)
Annotate an injectable class by specifying the class name and the named types of its dependencies...
Definition: Injectable.hpp:55
std::string getAddress() const
Get the address being listened on.
Definition: HttpServer.hpp:219
A WebSocket web application handler that routes to other handlers.
Balau::U8String< AllocatorT > toString(LoggingLevel level)
Print the logging level as a UTF-8 string.
Definition: LoggingLevel.hpp:73
Base interface of clocks.