forked from wfrest/wfrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_aop.cc
More file actions
105 lines (89 loc) · 2.25 KB
/
Copy path18_aop.cc
File metadata and controls
105 lines (89 loc) · 2.25 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 "workflow/WFFacilities.h"
#include <csignal>
#include "wfrest/HttpServer.h"
#include "wfrest/Aspect.h"
using namespace wfrest;
static WFFacilities::WaitGroup wait_group(1);
// Logging aspect
struct LogAop : public Aspect
{
bool before(const HttpReq *req, HttpResp *resp) override
{
fprintf(stderr, "before log\n");
return true;
}
bool after(const HttpReq *req, HttpResp *resp) override
{
fprintf(stderr, "After log\n");
return true;
}
};
struct OtherAop : public Aspect
{
bool before(const HttpReq *req, HttpResp *resp) override
{
fprintf(stderr, "before other\n");
return true;
}
// 'after()' should be called after reply
bool after(const HttpReq *req, HttpResp *resp) override
{
fprintf(stderr, "After other\n");
fprintf(stderr, "state : %d\terror : %d\n",
resp->get_state(), resp->get_error());
return true;
}
};
// transfer data from aspect to http handler
struct TransferAop : public Aspect
{
bool before(const HttpReq *req, HttpResp *resp) override
{
auto *content = new std::string("transfer data");
resp->user_data = content;
return true;
}
// If resp's 'user_data' needs to be deleted, delete it in 'after()'.
bool after(const HttpReq *req, HttpResp *resp) override
{
delete static_cast<std::string *>(resp->user_data);
return true;
}
};
void sig_handler(int signo)
{
wait_group.done();
}
int main()
{
signal(SIGINT, sig_handler);
HttpServer svr;
svr.GET("/no_aop", [](const HttpReq *req, HttpResp *resp)
{
resp->String("no aop");
});
svr.GET("/aop", [](const HttpReq *req, HttpResp *resp)
{
resp->String("aop");
}, LogAop());
svr.GET("/more_aop", [](const HttpReq *req, HttpResp *resp)
{
resp->String("more aop");
}, LogAop(), OtherAop());
svr.GET("/data", [](const HttpReq *req, HttpResp *resp)
{
auto *content = static_cast<std::string *>(resp->user_data);
resp->String(std::move(*content));
}, TransferAop());
if (svr.start(8888) == 0)
{
svr.list_routes();
wait_group.wait();
svr.stop();
} else
{
fprintf(stderr, "Cannot start server");
exit(1);
}
return 0;
}