|
| 1 | +// Copyright 2009 (c) Tarro, Inc. |
| 2 | +// Copyright 2009 (c) Dean Michael Berris <mikhailberis@gmail.com> |
| 3 | +// Copyright 2011 (c) Matt Leisinger <leisinger@gmail.com> |
| 4 | +// Distributed under the Boost Software License, Version 1.0. |
| 5 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 7 | +// |
| 8 | + |
| 9 | +//[ hello_world_server_main |
| 10 | +/*` |
| 11 | + This is a part of the 'Hello World' example. It's used to |
| 12 | + demonstrate how easy it is to set up an HTTP server. All we do in |
| 13 | + this example is create a request handler and run the server. This |
| 14 | + version of the server simply adds logging |
| 15 | +*/ |
| 16 | +#include<iostream> |
| 17 | + |
| 18 | +#include<boost/network/protocol/http/server.hpp> |
| 19 | + |
| 20 | +#include<boost/log/common.hpp> |
| 21 | +#include<boost/log/formatters.hpp> |
| 22 | +#include<boost/log/filters.hpp> |
| 23 | + |
| 24 | +#include<boost/log/utility/init/to_file.hpp> |
| 25 | +#include<boost/log/utility/init/to_console.hpp> |
| 26 | +#include<boost/log/utility/init/common_attributes.hpp> |
| 27 | + |
| 28 | +#include<boost/log/attributes.hpp> |
| 29 | +#include<boost/log/attributes/timer.hpp> |
| 30 | + |
| 31 | +namespacehttp= boost::network::http; |
| 32 | +namespacelogging= boost::log; |
| 33 | +namespacefmt= boost::log::formatters; |
| 34 | +namespaceflt= boost::log::filters; |
| 35 | +namespacesinks= boost::log::sinks; |
| 36 | +namespaceattrs= boost::log::attributes; |
| 37 | +namespacesrc= boost::log::sources; |
| 38 | +namespacekeywords= boost::log::keywords; |
| 39 | + |
| 40 | +/*<< Defines the server. >>*/ |
| 41 | +structhello_world; |
| 42 | +typedef http::server<hello_world> server; |
| 43 | + |
| 44 | +/*<< Defines the request handler. It's a class that defines two |
| 45 | + functions, `operator()` and `log()` >>*/ |
| 46 | +structhello_world { |
| 47 | +/*<< This is the function that handles the incoming request. >>*/ |
| 48 | +voidoperator() (server::requestconst &request, |
| 49 | + server::response &response) { |
| 50 | + server::string_type ip =source(request); |
| 51 | + std::ostringstream data; |
| 52 | + data <<"Hello," << ip <<"!"; |
| 53 | + response =server::response::stock_reply( |
| 54 | + server::response::ok, data.str()); |
| 55 | + } |
| 56 | +/*<< Using Boost.Log we'll simply log the number of errors >>*/ |
| 57 | +voidlog(constchar *error) { |
| 58 | +BOOST_LOG(lg) <<"Error occured (" << error <<"). Total number of errors reported is" << ++numErrors; |
| 59 | + } |
| 60 | + |
| 61 | +/*<< Catch all for any other errors >>*/ |
| 62 | +voidlog(...) { |
| 63 | +BOOST_LOG(lg) <<"Unknown error occured. Total number of error reported is" << ++numErrors; |
| 64 | + } |
| 65 | +private: |
| 66 | + src::logger_mt lg; |
| 67 | +uint32_t numErrors; |
| 68 | + |
| 69 | +}; |
| 70 | + |
| 71 | +/*<< Sets up two logger sinks, one to the console and the other to a file >>*/ |
| 72 | +voidinit_logging() |
| 73 | +{ |
| 74 | +/*<< Setup the console logger >>*/ |
| 75 | +logging::init_log_to_console(std::clog, keywords::format ="%TimeStamp%: %_%"); |
| 76 | + |
| 77 | +/*<< Setup the file logger >>*/ |
| 78 | + logging::init_log_to_file |
| 79 | + ( |
| 80 | +"hello_world_server.log", |
| 81 | + keywords::format =fmt::format("[%1%] %2%") |
| 82 | + %fmt::date_time("TimeStamp", std::nothrow) |
| 83 | + %fmt::message(), |
| 84 | + keywords::auto_flush =true |
| 85 | + ); |
| 86 | + |
| 87 | +logging::add_common_attributes(); |
| 88 | +} |
| 89 | + |
| 90 | +intmain(int argc,char * argv[]) { |
| 91 | + |
| 92 | +if (argc !=3) { |
| 93 | + std::cerr <<"Usage:" << argv[0] <<" address port" << std::endl; |
| 94 | +return1; |
| 95 | + } |
| 96 | + |
| 97 | +init_logging(); |
| 98 | + src::logger_mt lg; |
| 99 | + |
| 100 | +try { |
| 101 | +/*<< Creates the request handler. >>*/ |
| 102 | + hello_world handler; |
| 103 | +/*<< Creates the server. >>*/ |
| 104 | + serverserver_(argv[1], argv[2], handler); |
| 105 | +/*<< Write to the log source >>*/ |
| 106 | +BOOST_LOG(lg) <<"Starting HTTP Hello World Server..."; |
| 107 | +/*<< Runs the server. >>*/ |
| 108 | + server_.run(); |
| 109 | + } |
| 110 | +catch (std::exception &e) { |
| 111 | + std::cerr << e.what() << std::endl; |
| 112 | +return1; |
| 113 | + } |
| 114 | + |
| 115 | +return0; |
| 116 | +} |
| 117 | +//] |