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