-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp_server.cpp
More file actions
105 lines (88 loc) · 2.93 KB
/
http_server.cpp
File metadata and controls
105 lines (88 loc) · 2.93 KB
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
104
105
#include "core/HttpServer.h"
#include "core/WebSocket.h"
#include <iostream>
using namespace hical;
int main(int argc, char* argv[])
{
try
{
auto port = static_cast<uint16_t>(argc >= 2 ? std::atoi(argv[1]) : 8080);
HttpServer server(port);
// ============ 中间件 ============
// 日志中间件
server.use(
[](HttpRequest& req, MiddlewareNext next) -> Awaitable<HttpResponse>
{
std::cout << httpMethodToString(req.method()) << " " << req.path() << std::endl;
auto res = co_await next(req);
std::cout << " -> " << static_cast<int>(res.statusCode()) << std::endl;
co_return res;
});
// ============ HTTP 路由 ============
// GET / — 首页
server.router().get("/",
[](const HttpRequest&) -> HttpResponse
{
return HttpResponse::ok("Welcome to hical!");
});
// GET /api/status — 状态查询
server.router().get("/api/status",
[](const HttpRequest&) -> HttpResponse
{
return HttpResponse::json(
{{"status", "running"}, {"version", "0.2.0"}, {"framework", "hical"}});
});
// POST /api/echo — Echo 回写
server.router().post("/api/echo",
[](const HttpRequest& req) -> HttpResponse
{
return HttpResponse::ok(req.body());
});
// GET /api/hello — 带查询参数示例
server.router().get("/api/hello",
[](const HttpRequest& req) -> HttpResponse
{
auto query = req.query();
if (query.empty())
{
return HttpResponse::ok("Hello, World!");
}
return HttpResponse::ok("Hello! query=" + std::string(query));
});
// GET /users/{id} — 路径参数示例
server.router().get("/users/{id}",
[](const HttpRequest& req) -> HttpResponse
{
return HttpResponse::json(
{{"userId", req.param("id")}, {"name", "User " + req.param("id")}});
});
// ============ WebSocket 路由 ============
// WebSocket Echo
server.router().ws(
"/ws/echo",
[](const std::string& msg, WebSocketSession& ws) -> Awaitable<void>
{
co_await ws.send("Echo: " + msg);
},
[](WebSocketSession& ws) -> Awaitable<void>
{
co_await ws.send("Connected to hical WebSocket!");
});
// ============ 启动 ============
std::cout << "hical HTTP Server v0.2.0" << std::endl;
std::cout << "监听端口: " << port << std::endl;
std::cout << "路由:" << std::endl;
std::cout << " GET / — 首页" << std::endl;
std::cout << " GET /api/status — 状态查询" << std::endl;
std::cout << " POST /api/echo — Echo 回写" << std::endl;
std::cout << " GET /api/hello — Hello 示例" << std::endl;
std::cout << " GET /users/{id} — 用户查询" << std::endl;
std::cout << " WS /ws/echo — WebSocket Echo" << std::endl;
server.start();
}
catch (const std::exception& e)
{
std::cerr << "异常: " << e.what() << std::endl;
}
return 0;
}