summaryrefslogtreecommitdiffhomepage
path: root/server.cpp
blob: 91ee9e831ac7a6aef6f1821b37c79306e2923324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>

#include <thread>
#include <vector>

#include "server.h"

#include "http.h"
#include "https.h"
#include "privileges.h"

namespace beast = boost::beast;         // from <boost/beast.hpp>
namespace http = beast::http;           // from <boost/beast/http.hpp>
namespace net = boost::asio;            // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl;       // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>

Server::Server(Config& config, boost::asio::io_context& ioc): m_config(config), m_ioc(ioc)
{
}

Server::~Server()
{
}

int server(Config& config)
{
 auto const threads = std::max<int>(1, config.Threads());

 boost::asio::io_context ioc{threads};

 std::vector<std::shared_ptr<Server>> servers;

 const auto& sockets {config.Sockets()};
 for (const auto& socket: sockets) {
  if (socket.protocol == SocketProtocol::HTTP) {
   servers.push_back(std::make_shared<HTTP::Server>(config, ioc, socket));
  } else {
   servers.push_back(std::make_shared<HTTPS::Server>(config, ioc, socket));
  }
  servers.back()->start();
 }

 // set UID, GID
 drop_privileges(config);

 // Run the I/O service on the requested number of threads
 std::vector<std::thread> v;
 v.reserve(threads - 1);
 for(auto i = threads - 1; i > 0; --i)
     v.emplace_back(
     [&ioc]
     {
         ioc.run();
     });
 ioc.run();

 return EXIT_SUCCESS; 
}