-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·40 lines (32 loc) · 1.02 KB
/
main.cpp
File metadata and controls
executable file
·40 lines (32 loc) · 1.02 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
#include <iostream>
using namespace std;
#include <web/server.h>
using namespace yazi::web;
void hello(const Request & req, Response & resp)// 处理 /hello 路径的 GET 请求
{
string name = req.get("name");
string age = req.get("age");
string host = req.user_host();
resp.data(Response::HTML, "hello, " + name + "," + age + "," + host);
}
void reply(const Request & req, Response & resp)// 处理 /reply 路径的 POST 请求
{
Json name = req.post("name");
Json age = req.post("age");
if (name.is_null())
{
// 姓名为空
}
Json json;
json["name"] = name;
json["age"] = age;
resp.data(Response::JSON, json.str());
}
int main()
{
auto server = Singleton<Server>::instance();// 获取 Server 单例
server->bind("/hello", hello); // 绑定 /hello 路径到 hello 函数
server->bind("/reply", reply); // 绑定 /reply 路径到 reply 函数
server->start();
return 0;
}