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/workers.hpp>
12 : #include <boost/capy/application.hpp>
13 : #include <boost/corosio/io_context.hpp>
14 : #include <boost/corosio/endpoint.hpp>
15 : #include <boost/url/ipv4_address.hpp>
16 :
17 : namespace boost {
18 : namespace beast2 {
19 :
20 : namespace {
21 :
22 : class http_server_impl
23 : : public http_server
24 : {
25 : public:
26 0 : http_server_impl(
27 : capy::application& app,
28 : char const* addr,
29 : unsigned short port,
30 : std::size_t num_workers)
31 0 : : ioc_()
32 0 : , workers_(app, ioc_, num_workers, this->wwwroot)
33 : {
34 : // Parse address and create endpoint
35 0 : auto addr_result = urls::parse_ipv4_address(addr);
36 0 : if (addr_result.has_error())
37 : {
38 : // Fallback to any address if parsing fails
39 0 : workers_.listen(
40 : http::acceptor_config{false, false},
41 : corosio::endpoint(port));
42 : }
43 : else
44 : {
45 0 : workers_.listen(
46 : http::acceptor_config{false, false},
47 0 : corosio::endpoint(addr_result.value(), port));
48 : }
49 0 : }
50 :
51 0 : void run() override
52 : {
53 0 : workers_.start();
54 0 : ioc_.run();
55 0 : }
56 :
57 0 : void stop() override
58 : {
59 0 : workers_.stop();
60 0 : ioc_.stop();
61 0 : }
62 :
63 : private:
64 : corosio::io_context ioc_;
65 : workers workers_;
66 : };
67 :
68 : } // (anon)
69 :
70 : //------------------------------------------------
71 :
72 : http_server&
73 0 : install_plain_http_server(
74 : capy::application& app,
75 : char const* addr,
76 : unsigned short port,
77 : std::size_t num_workers)
78 : {
79 0 : return app.emplace<http_server_impl>(
80 0 : app, addr, port, num_workers);
81 : }
82 :
83 : } // beast2
84 : } // boost
|