http_main.cc
1 // Copyright 2013, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #include "util/asio/accept_server.h"
5 #include "util/asio/io_context_pool.h"
6 
7 #include "util/http/http_conn_handler.h"
8 #include "util/html/sorted_table.h"
9 #include "util/stats/varz_stats.h"
10 
11 #include "absl/strings/str_join.h"
12 #include "base/init.h"
13 #include "strings/stringpiece.h"
14 
15 DEFINE_int32(port, 8080, "Port number.");
16 
17 using namespace std;
18 using namespace boost;
19 using namespace util;
20 namespace h2 = beast::http;
21 
22 VarzQps http_qps("bar-qps");
23 
24 
25 int main(int argc, char** argv) {
26  MainInitGuard guard(&argc, &argv);
27 
28  IoContextPool pool;
29  AcceptServer server(&pool);
30  pool.Run();
31  http::Listener<> listener;
32  auto cb = [](const http::QueryArgs& args, http::HttpHandler::SendFunction* send) {
33  http::StringResponse resp = http::MakeStringResponse(h2::status::ok);
34  resp.body() = "Bar";
35 
36  http::SetMime(http::kTextMime, &resp);
37  resp.set(beast::http::field::server, "GAIA");
38  http_qps.Inc();
39 
40  return send->Invoke(std::move(resp));
41  };
42 
43  listener.RegisterCb("/foo", false, cb);
44 
45  auto table_cb = [](const http::QueryArgs& args, http::HttpHandler::SendFunction* send) {
46  using html::SortedTable;
47 
48  auto cell = [](auto i, auto j) {
49  return absl::StrCat("Val", i, "_", j);
50  };
51 
52 
53  http::StringResponse resp = http::MakeStringResponse(h2::status::ok);
54  resp.body() = SortedTable::HtmlStart();
55  SortedTable::StartTable({"Col1", "Col2", "Col3", "Col4"}, &resp.body());
56  for (size_t i = 0; i < 300; ++i) {
57  SortedTable::Row({cell(1, i), cell(2, i), cell(3, i), cell(4, i)}, &resp.body());
58  }
59  SortedTable::EndTable(&resp.body());
60  return send->Invoke(std::move(resp));
61  };
62  listener.RegisterCb("/table", false, table_cb);
63 
64 
65  uint16_t port = server.AddListener(FLAGS_port, &listener);
66  LOG(INFO) << "Listening on port " << port;
67 
68  server.Run();
69  server.Wait();
70 
71  LOG(INFO) << "Exiting server...";
72  return 0;
73 }
void Run()
Starts running all IoContext objects in the pool. Does not block.
A pool of IoContext objects, representing and managing CPU resources of the system.