-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_http.cc
53 lines (44 loc) · 1.56 KB
/
example_http.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
/*************************************************************************
@Copyright (c), ke Inc
@Created Time : 2021-02-24 08:15:43 PM
@Created By : dongzhixiong
@Description:
@Last modified: 2021-08-03 05:26:02 PM
@Modified By: dongzhixiong
************************************************************************/
#include <iostream>
#include <random>
#include <unistd.h>
#include <cmath>
#include "service/http_server.h"
void DefaultHandler(const kbms::HttpContextPtr &ctx, const kbms::HTTPSendResponseCallback &cb) {
cb(std::string("this default handler"));
}
void ExpHandler(const kbms::HttpContextPtr &ctx, const kbms::HTTPSendResponseCallback &cb) {
cb(std::string("operation is timeout"));
}
void Func(const kbms::HttpContextPtr &ctx, const kbms::HTTPSendResponseCallback &cb) {
std::string request_body = ctx->BodyByString();
Json::Value result;
result["code"] = 1;
result["msg"] = "success";
result["query"] = request_body;
result["data"] = request_body.size();
cb(result.toStyledString());
}
int main() {
uint32_t port = 12138;
uint32_t thread_num = 1;
if (!kbms::HttpServer::Instance()->Init(port, thread_num)) {
std::cerr << "initalize global resource failed." << std::endl;
return 0;
}
kbms::HttpServer::Instance()->RegisterDefaultFun(&DefaultHandler);
kbms::HttpServer::Instance()->RegisterExpireFun(&ExpHandler);
kbms::HttpServer::Instance()->RegisterFun("/echo", &Func);
kbms::HttpServer::Instance()->StartServer();
while (true) {
usleep(1);
}
return 0;
}