-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhttp_main.cc
73 lines (54 loc) · 1.93 KB
/
http_main.cc
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
68
69
70
71
72
73
// Copyright 2013, Beeri 15. All rights reserved.
// Author: Roman Gershman ([email protected])
//
#include "util/asio/accept_server.h"
#include "util/asio/io_context_pool.h"
#include "util/http/http_conn_handler.h"
#include "util/html/sorted_table.h"
#include "util/stats/varz_stats.h"
#include "absl/strings/str_join.h"
#include "base/init.h"
#include "strings/stringpiece.h"
DEFINE_int32(port, 8080, "Port number.");
using namespace std;
using namespace boost;
using namespace util;
namespace h2 = beast::http;
VarzQps http_qps("bar-qps");
int main(int argc, char** argv) {
MainInitGuard guard(&argc, &argv);
IoContextPool pool;
AcceptServer server(&pool);
pool.Run();
http::Listener<> listener;
auto cb = [](const http::QueryArgs& args, http::HttpHandler::SendFunction* send) {
http::StringResponse resp = http::MakeStringResponse(h2::status::ok);
resp.body() = "Bar";
http::SetMime(http::kTextMime, &resp);
resp.set(beast::http::field::server, "GAIA");
http_qps.Inc();
return send->Invoke(std::move(resp));
};
listener.RegisterCb("/foo", false, cb);
auto table_cb = [](const http::QueryArgs& args, http::HttpHandler::SendFunction* send) {
using html::SortedTable;
auto cell = [](auto i, auto j) {
return absl::StrCat("Val", i, "_", j);
};
http::StringResponse resp = http::MakeStringResponse(h2::status::ok);
resp.body() = SortedTable::HtmlStart();
SortedTable::StartTable({"Col1", "Col2", "Col3", "Col4"}, &resp.body());
for (size_t i = 0; i < 300; ++i) {
SortedTable::Row({cell(1, i), cell(2, i), cell(3, i), cell(4, i)}, &resp.body());
}
SortedTable::EndTable(&resp.body());
return send->Invoke(std::move(resp));
};
listener.RegisterCb("/table", false, table_cb);
uint16_t port = server.AddListener(FLAGS_port, &listener);
LOG(INFO) << "Listening on port " << port;
server.Run();
server.Wait();
LOG(INFO) << "Exiting server...";
return 0;
}