Line data Source code
1 : #ifndef THINGER_HTTP_SERVER_STANDALONE_HPP
2 : #define THINGER_HTTP_SERVER_STANDALONE_HPP
3 :
4 : #include "http_server_base.hpp"
5 : #include <boost/asio/io_context.hpp>
6 : #include <boost/asio/executor_work_guard.hpp>
7 :
8 : namespace thinger::http {
9 :
10 : /**
11 : * Standalone HTTP server that uses its own io_context.
12 : * Perfect for simple applications that don't need the full worker infrastructure.
13 : * Single-threaded server that runs in the thread that calls wait().
14 : */
15 : class server : public http_server_base {
16 : private:
17 : // Own io_context for handling async operations
18 : boost::asio::io_context io_context_;
19 :
20 : // Work guard to keep io_context running
21 : using work_guard_type = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;
22 : std::unique_ptr<work_guard_type> work_guard_;
23 :
24 : std::atomic<bool> running_{false};
25 :
26 : protected:
27 : // Create socket server with our io_context
28 : std::unique_ptr<asio::socket_server> create_socket_server(
29 : const std::string& host, const std::string& port) override;
30 :
31 : // Create Unix socket server with our io_context
32 : std::unique_ptr<asio::unix_socket_server> create_unix_socket_server(
33 : const std::string& unix_path) override;
34 :
35 : public:
36 : // Constructor - single threaded server
37 : server();
38 : ~server() override;
39 :
40 : // Override listen to setup server
41 : bool listen(const std::string& host, uint16_t port) override;
42 : bool listen_unix(const std::string& unix_path) override;
43 :
44 : // Wait for server to stop
45 : void wait() override;
46 :
47 : // Stop the server
48 : bool stop() override;
49 :
50 : // Get direct access to io_context for advanced use cases
51 3 : boost::asio::io_context& get_io_context() {
52 3 : return io_context_;
53 : }
54 : };
55 :
56 : } // namespace thinger::http
57 :
58 : #endif // THINGER_HTTP_SERVER_STANDALONE_HPP
|