-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoHttpServer.cc
103 lines (90 loc) · 3.57 KB
/
DemoHttpServer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Created by zaxtyson on 2021/9/25.
//
#include <logger/Logger.h>
#include <proto/http/HttpServer.h>
using namespace jerry;
using namespace jerry::proto::http;
using namespace std::chrono_literals;
class DemoHttpServer : public HttpServer {
void OnConnected(net::TcpConn* conn, const DateTime& time) override {
LOG_INFO(
"%s connected at %s", conn->GetPeerAddress().GetHost().data(), time.ToString().data())
}
void OnRequest(const HttpReq& req,
HttpResp& resp,
net::TcpConn* conn,
const DateTime& time) override {
LOG_INFO("%s | %s %s %s | %s | body %zu bytes",
conn->GetPeerAddress().GetHost().data(),
req.GetMethodString().data(),
req.GetRequestUri().data(),
req.GetVersionString().data(),
time.ToString().data(),
req.GetBody().size())
LOG_INFO("%s", req.GetHeader("User-Agent").value_or("no user-agent").data())
resp.SetServer("Jerry/0.5");
resp.SetContentType("text/html; charset=utf-8");
char buf[8192]{};
auto last_time = conn->GetContext<DateTime>("last_time").value_or(DateTime::Now());
snprintf(buf,
sizeof(buf),
R"(
<html>
<head>
<title>Demo</title>
</head>
<body>
<h1>Hello, This is Jerry!</h1>
<p>Server received request: %s</p>
<p>Last visit time: %s</p>
<hr>
<div>
<h4>Your Request</h4>
<pre>%s</pre>
</div>
</body>
</html>
)",
time.ToString().data(),
last_time.ToString().data(),
req.ToString().data());
resp.SetBody(buf);
conn->SetContext("last_time", time);
}
void OnDisConnected(net::TcpConn* conn, const DateTime& time) override {
LOG_INFO("%s disconnected at %s",
conn->GetPeerAddress().GetHost().data(),
time.ToString().data())
}
void Monitor() override {
std::this_thread::sleep_for(std::chrono::seconds(3));
printf("[Health Check] Total connections: %zu, Total timers: %zu, Pending tasks: %zu\n",
GetTotalConns(),
GetTotalTimers(),
GetPendingTasks());
}
};
int main() {
auto* appender = new logger::AsyncFileAppender("app.log");
appender->Start(); // start an async logger thread
Logger::SetAppender(appender);
Logger::SetLogLevel(LogLevel::kInfo);
net::ServerConfig config;
config.acceptor.listen_ip = "127.0.0.1";
config.acceptor.listen_port = 8443;
config.workgroup.workers = 1;
config.threadpool.workers = 0;
config.threadpool.pending_size = 0;
// to generate certification: https://mkcert.dev/
// mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1
config.ssl.use_ssl = true;
config.ssl.disable_old_ssl_version = true;
config.ssl.enable_validation = true;
config.ssl.cert_file = "./pem/cert.pem";
config.ssl.private_key_file = "./pem/key.pem";
DemoHttpServer server;
server.Config(config);
server.Serve();
appender->Stop();
}