proxygen
StaticServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 
11 #include <folly/Memory.h>
14 #include <folly/init/Init.h>
20 
21 #include "StaticHandler.h"
22 
23 using namespace StaticService;
24 using namespace proxygen;
25 
26 using folly::EventBase;
29 
31 
32 DEFINE_int32(http_port, 11000, "Port to listen on with HTTP protocol");
33 DEFINE_int32(h2_port, 11002, "Port to listen on with HTTP/2 protocol");
34 DEFINE_string(ip, "localhost", "IP/Hostname to bind to");
35 DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
36  "will use the number of cores on this machine.");
37 
38 namespace {
39 
40 class StaticHandlerFactory : public RequestHandlerFactory {
41  public:
42  void onServerStart(folly::EventBase* /*evb*/) noexcept override {}
43 
44  void onServerStop() noexcept override {}
45 
46  RequestHandler* onRequest(RequestHandler*, HTTPMessage*) noexcept override {
47  return new StaticHandler;
48  }
49 };
50 
51 }
52 
53 int main(int argc, char* argv[]) {
54  folly::init(&argc, &argv, true);
55 
56  std::vector<HTTPServer::IPConfig> IPs = {
57  {SocketAddress(FLAGS_ip, FLAGS_http_port, true), Protocol::HTTP},
58  {SocketAddress(FLAGS_ip, FLAGS_h2_port, true), Protocol::HTTP2},
59  };
60 
61  if (FLAGS_threads <= 0) {
62  FLAGS_threads = sysconf(_SC_NPROCESSORS_ONLN);
63  CHECK_GT(FLAGS_threads, 0);
64  }
65 
66  HTTPServerOptions options;
67  options.threads = static_cast<size_t>(FLAGS_threads);
68  options.idleTimeout = std::chrono::milliseconds(60000);
69  options.shutdownOn = {SIGINT, SIGTERM};
70  options.enableContentCompression = false;
72  .addThen<StaticHandlerFactory>()
73  .build();
74  options.h2cEnabled = true;
75 
76  auto diskIOThreadPool = std::make_shared<folly::CPUThreadPoolExecutor>(
77  FLAGS_threads,
78  std::make_shared<folly::NamedThreadFactory>("StaticDiskIOThread"));
79  folly::setCPUExecutor(diskIOThreadPool);
80 
81  HTTPServer server(std::move(options));
82  server.bind(IPs);
83 
84  // Start HTTPServer mainloop in a separate thread
85  std::thread t([&] () {
86  server.start();
87  });
88 
89  t.join();
90  return 0;
91 }
int main(int argc, char *argv[])
void start(std::function< void()> onSuccess=nullptr, std::function< void(std::exception_ptr)> onError=nullptr)
Definition: HTTPServer.cpp:119
std::chrono::milliseconds idleTimeout
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void bind(std::vector< IPConfig > &&addrs)
Definition: HTTPServer.cpp:85
RequestHandlerChain & addThen(Args &&...args)
requires E e noexcept(noexcept(s.error(std::move(e))))
HTTPServer::Protocol Protocol
std::vector< std::thread::id > threads
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
DEFINE_string(ip,"localhost","IP/Hostname to bind to")
char ** argv
std::vector< std::unique_ptr< RequestHandlerFactory > > handlerFactories
DEFINE_int32(http_port, 11000,"Port to listen on with HTTP protocol")
std::vector< int > shutdownOn
void setCPUExecutor(std::weak_ptr< Executor > executor)