forked from daedric/httpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.cpp
67 lines (59 loc) · 1.63 KB
/
ping.cpp
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
67
/*
* Part of HTTPP.
*
* Distributed under the 2-clause BSD licence (See LICENCE.TXT file at the
* project root).
*
* Copyright (c) 2015 Thomas Sanchez. All rights reserved.
*
*/
#include <chrono>
#include <iostream>
#include <string>
#include <httpp/HttpServer.hpp>
#include <httpp/http/Utils.hpp>
#include <httpp/utils/Exception.hpp>
using HTTPP::HttpServer;
using HTTPP::HTTP::Connection;
using HTTPP::HTTP::HttpCode;
using HTTPP::HTTP::Request;
using HTTPP::HTTP::helper::ReadWholeRequest;
void handler(Connection* connection)
{
read_whole_request(connection, [](std::unique_ptr<ReadWholeRequest> handle,
const boost::system::error_code& ec) {
if (ec)
{
throw HTTPP::UTILS::convert_boost_ec_to_std_ec(ec);
}
auto connection = handle->connection;
connection->response().setCode(HttpCode::Ok);
HTTPP::HTTP::setShouldConnectionBeClosed(connection->request(),
connection->response());
connection->sendResponse(); // connection pointer may become invalid
});
}
int main(int ac, char** av)
{
std::string port = "8080";
if (ac > 1)
{
port = av[1];
}
else
{
auto port_env = getenv("PORT");
if (port_env)
{
port = port_env;
}
}
commonpp::core::init_logging();
commonpp::core::set_logging_level(commonpp::warning);
HttpServer server(1);
server.start();
server.setSink(&handler);
server.bind("0.0.0.0", port);
while (true)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}