Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/beast2
8 : //
9 :
10 : #include <boost/beast2/server/http_server.hpp>
11 : #include <boost/beast2/server/http_session.hpp>
12 : #include <boost/beast2/server/plain_worker.hpp>
13 : #include <boost/beast2/application.hpp>
14 : #include <boost/beast2/asio_io_context.hpp>
15 :
16 : namespace boost {
17 : namespace beast2 {
18 :
19 : namespace {
20 :
21 : template<class Stream>
22 : class http_server_impl
23 : : public http_server<Stream>
24 : {
25 : public:
26 0 : http_server_impl(
27 : application& app,
28 : std::size_t num_workers)
29 0 : : ioc_(install_single_threaded_asio_io_context(app))
30 0 : , w_(app,
31 0 : ioc_.get_executor(), 1, num_workers,
32 0 : ioc_.get_executor(), this->wwwroot)
33 : {
34 0 : }
35 :
36 0 : void add_port(
37 : char const* addr,
38 : unsigned short port)
39 : {
40 0 : w_.emplace(
41 : acceptor_config{ false, false },
42 0 : asio::ip::tcp::endpoint(
43 : asio::ip::make_address(addr),
44 : port),
45 0 : true);
46 0 : }
47 :
48 0 : void start()
49 : {
50 0 : w_.start();
51 0 : }
52 :
53 0 : void stop()
54 : {
55 0 : w_.stop();
56 0 : }
57 :
58 0 : void attach() override
59 : {
60 0 : ioc_.attach();
61 0 : }
62 :
63 : private:
64 : using workers_type = workers< plain_worker<
65 : asio::io_context::executor_type, asio::ip::tcp> >;
66 :
67 : asio_io_context& ioc_;
68 : workers_type w_;
69 : };
70 :
71 : } // (anon)
72 :
73 : //------------------------------------------------
74 :
75 : auto
76 0 : install_plain_http_server(
77 : application& app,
78 : char const* addr,
79 : unsigned short port,
80 : std::size_t num_workers) ->
81 : http_server<asio::basic_stream_socket<
82 : asio::ip::tcp,
83 : asio::io_context::executor_type>>&
84 : {
85 : using stream_type = asio::basic_stream_socket<
86 : asio::ip::tcp, asio::io_context::executor_type>;
87 0 : auto& srv = app.emplace<http_server_impl<stream_type>>(
88 : app, num_workers);
89 0 : srv.add_port(addr, port);
90 0 : return srv;
91 : }
92 :
93 : } // beast2
94 : } // boost
95 :
|