-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
59 lines (42 loc) · 1.49 KB
/
Server.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
#include <unistd.h>
#include <folly/Memory.h>
#include <folly/executors/IOThreadPoolExecutor.h>
#include <folly/io/async/EventBaseManager.h>
#include <proxygen/httpserver/HTTPServer.h>
#include <proxygen/httpserver/RequestHandlerFactory.h>
#include "HelloHandler.h"
using namespace proxygen;
using namespace hello;
class HelloHandlerFactory : public RequestHandlerFactory {
public:
HelloHandlerFactory(size_t size)
: cpuPool_(std::make_unique<folly::CPUThreadPoolExecutor>(size)) {}
~HelloHandlerFactory() {}
void onServerStart(folly::EventBase *evb) noexcept override {}
void onServerStop() noexcept override {}
RequestHandler *onRequest(RequestHandler *, HTTPMessage *) noexcept override {
return new HelloHandler(cpuPool_.get());
}
protected:
std::unique_ptr<folly::CPUThreadPoolExecutor> cpuPool_;
};
int main(int argc, char *argv[]) {
size_t threads = sysconf(_SC_NPROCESSORS_ONLN);
std::vector<HTTPServer::IPConfig> IPs = {
{folly::SocketAddress("localhost", 3000, true),
HTTPServer::Protocol::HTTP}};
HTTPServerOptions options;
options.threads = threads;
options.idleTimeout = std::chrono::milliseconds(60000);
options.shutdownOn = {SIGINT, SIGTERM};
options.enableContentCompression = false;
options.handlerFactories =
RequestHandlerChain()
.addThen<HelloHandlerFactory>(threads)
.build();
HTTPServer server(std::move(options));
server.bind(IPs);
std::thread t([&]() { server.start(); });
t.join();
return 0;
}