forked from qicosmos/cinatra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
249 lines (210 loc) · 7.41 KB
/
main.cpp
File metadata and controls
249 lines (210 loc) · 7.41 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include "../include/cinatra.hpp"
using namespace cinatra;
struct log_t
{
bool before(request& req, response& res) {
std::cout << "before log" << std::endl;
return true;
}
bool after(request& req, response& res) {
std::cout << "after log" << std::endl;
res.add_header("aaaa", "bbcc");
return true;
}
};
struct check {
bool before(request& req, response& res) {
std::cout << "before check" << std::endl;
if (req.get_header_value("name").empty()) {
res.render_404();
return false;
}
return true;
}
bool after(request& req, response& res) {
std::cout << "after check" << std::endl;
return true;
}
};
struct person
{
void foo(request& req, response& res)
{
std::cout << i << std::endl;
res.render_string("ok");
}
void foo1(request& req, response& res)
{
std::cout << i << std::endl;
res.render_string("ok");
}
int i=0;
};
int main() {
const int max_thread_num = 4;
http_server server(max_thread_num);
#ifdef CINATRA_ENABLE_SSL
server.init_ssl_context(true, [](auto, auto) { return "123456"; }, "server.crt", "server.key", "dh1024.pem");
bool r = server.listen("0.0.0.0", "https");
#else
bool r = server.listen("0.0.0.0", "8080");
#endif
if (!r) {
//LOG_INFO << "listen failed";
return -1;
}
server.set_public_root_directory("");
server.set_base_path("base_path","/feather");
server.enable_http_cache(false);//set global cache
server.set_res_cache_max_age(86400);
server.set_cache_max_age(5);
server.set_http_handler<GET, POST>("/", [](request& req, response& res) {
res.set_status_and_content(status_type::ok,"hello world");
},enable_cache{false});
person p{ 2 };
server.set_http_handler<GET, POST>("/a", &person::foo, enable_cache{ false }, log_t{});
// server.set_http_handler<GET, POST>("/b", &person::foo1, log_t{}, enable_cache{ false });
server.set_http_handler<GET, POST>("/string", [](request& req, response& res) {
res.render_string(std::to_string(std::time(nullptr)));
},enable_cache{true});
server.set_http_handler<GET, POST>("/404", [](request& req, response& res) {
res.render_404();
},enable_cache{false});
server.set_http_handler<GET, POST>("/404_custom", [](request& req, response& res) {
res.render_404("./404.html");
},enable_cache{false});
server.set_http_handler<GET, POST>("/login", [](request& req, response& res) {
auto session = res.start_session();
session->set_data("userid", std::string("1"));
session->set_max_age(-1);
res.set_status_and_content(status_type::ok, "login");
},enable_cache{false});
server.set_http_handler<GET, POST>("/islogin", [](request& req, response& res) {
auto ptr = req.get_session();
auto session = ptr.lock();
if (session == nullptr || session->get_data<std::string>("userid") != "1") {
res.set_status_and_content(status_type::ok, "没有登录", res_content_type::string);
return;
}
res.set_status_and_content(status_type::ok, "已经登录", res_content_type::string);
},enable_cache{false});
server.set_http_handler<GET, POST>("/html", [](request& req, response& res) {
res.set_attr("number",1024);
res.set_attr("test_text","hello,world");
res.set_attr("header_text","你好 cinatra");
res.render_view("./www/test.html");
});
server.set_http_handler<GET, POST,OPTIONS>("/json", [](request& req, response& res) {
nlohmann::json json;
res.add_header("Access-Control-Allow-Origin","*");
if(req.get_method()=="OPTIONS"){
res.add_header("Access-Control-Allow-Headers","Authorization");
res.render_string("");
}else{
json["abc"] = "abc";
json["success"] = true;
json["number"] = 100.005;
json["name"] = "中文";
json["time_stamp"] = std::time(nullptr);
res.render_json(json);
}
});
server.set_http_handler<GET,POST>("/redirect",[](request& req, response& res){
res.redirect("http://www.baidu.com"); // res.redirect("/json");
});
server.set_http_handler<GET, POST>("/pathinfo/*", [](request& req, response& res) {
auto s = req.get_query_value(0);
res.render_string(std::string(s.data(), s.length()));
});
server.set_http_handler<GET, POST>("/restype", [](request& req, response& res) {
auto type = req.get_query_value("type");
auto res_type = cinatra::res_content_type::string;
if (type == "html")
{
res_type = cinatra::res_content_type::html;
}
else if (type == "json") {
res_type = cinatra::res_content_type::json;
}
else if (type == "string") {
//do not anything;
}
res.set_status_and_content(status_type::ok, "<a href='http://www.baidu.com'>hello world 百度</a>", res_type);
});
server.set_http_handler<GET, POST>("/getzh", [](request& req, response& res) {
auto zh = req.get_query_value("zh");
res.render_string(std::string(zh.data(),zh.size()));
});
server.set_http_handler<GET, POST>("/gzip", [](request& req, response& res) {
auto body = req.body();
std::cout << body.data() << std::endl;
res.set_status_and_content(status_type::ok, "hello world", res_content_type::none, content_encoding::gzip);
});
server.set_http_handler<GET, POST>("/test", [](request& req, response& res) {
auto name = req.get_header_value("name");
if (name.empty()) {
res.render_string("no name");
return;
}
auto id = req.get_query_value("id");
if (id.empty()) {
res.render_404();
return;
}
res.render_string("hello world");
});
//aspect
server.set_http_handler<GET, POST>("/aspect", [](request& req, response& res) {
res.render_string("hello world");
}, check{}, log_t{});
//web socket
server.set_http_handler<GET, POST>("/ws", [](request& req, response& res) {
assert(req.get_content_type() == content_type::websocket);
req.on(ws_open, [](request& req){
std::cout << "websocket start" << std::endl;
});
req.on(ws_message, [](request& req) {
auto part_data = req.get_part_data();
//echo
std::string str = std::string(part_data.data(), part_data.length());
req.get_conn()->send_ws_string(std::move(str));
std::cout << part_data.data() << std::endl;
});
req.on(ws_close, [](request& req) {
std::cout << "websocket close" << std::endl;
});
req.on(ws_error, [](request& req) {
std::cout << "websocket error" << std::endl;
});
});
server.set_http_handler<GET, POST>("/vue_html", [](request& req, response& res) {
res.render_raw_view("./www/index.html");
});
server.set_http_handler<GET, POST>("/vue_demo", [](request& req, response& res) {
res.render_raw_view("./www/dist/index.html");
});
//http upload(multipart)
server.set_http_handler<GET, POST>("/upload_multipart", [](request& req, response& res) {
assert(req.get_content_type() == content_type::multipart);
auto& files = req.get_upload_files();
for (auto& file : files) {
std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
}
res.render_string("multipart finished");
});
//http upload(octet-stream)
server.set_http_handler<GET, POST>("/upload_octet_stream", [](request& req, response& res) {
assert(req.get_content_type() == content_type::octet_stream);
auto& files = req.get_upload_files();
for (auto& file : files) {
std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
}
res.render_string("octet-stream finished");
});
//chunked download
//http://127.0.0.1:8080/assets/show.jpg
//cinatra will send you the file, if the file is big file(more than 5M) the file will be downloaded by chunked
server.run();
return 0;
}