Files
boost_beast/examples/http_server.cpp

62 lines
1.9 KiB
C++
Raw Normal View History

//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 08:01:46 -07:00
#include "http_async_server.hpp"
#include "http_sync_server.hpp"
2017-07-20 08:01:46 -07:00
#include <beast/test/sig_wait.hpp>
2017-07-20 08:01:46 -07:00
#include <boost/program_options.hpp>
#include <iostream>
int main(int ac, char const* av[])
{
using namespace beast::http;
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
2016-10-15 20:48:59 +03:00
("root,r", po::value<std::string>()->default_value("."),
2017-07-20 08:01:46 -07:00
"Set the root directory for serving files")
2016-10-15 20:48:59 +03:00
("port,p", po::value<std::uint16_t>()->default_value(8080),
2017-07-20 08:01:46 -07:00
"Set the port number for the server")
2016-10-15 20:48:59 +03:00
("ip", po::value<std::string>()->default_value("0.0.0.0"),
2017-07-20 08:01:46 -07:00
"Set the IP address to bind to, \"0.0.0.0\" for all")
2016-10-15 20:48:59 +03:00
("threads,n", po::value<std::size_t>()->default_value(4),
2017-07-20 08:01:46 -07:00
"Set the number of threads to use")
("sync,s", "Launch a synchronous server")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
2016-10-15 20:48:59 +03:00
std::string root = vm["root"].as<std::string>();
2017-07-20 08:01:46 -07:00
2016-10-15 20:48:59 +03:00
std::uint16_t port = vm["port"].as<std::uint16_t>();
2017-07-20 08:01:46 -07:00
2016-10-15 20:48:59 +03:00
std::string ip = vm["ip"].as<std::string>();
2017-07-20 08:01:46 -07:00
2016-10-15 20:48:59 +03:00
std::size_t threads = vm["threads"].as<std::size_t>();
2017-07-20 08:01:46 -07:00
bool sync = vm.count("sync") > 0;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
endpoint_type ep{address_type::from_string(ip), port};
if(sync)
{
2017-07-20 08:01:46 -07:00
http_sync_server server(ep, root);
beast::test::sig_wait();
}
2017-07-20 08:01:46 -07:00
else
{
2017-07-20 08:01:46 -07:00
http_async_server server(ep, threads, root);
beast::test::sig_wait();
}
2017-07-20 08:01:46 -07:00
}