-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
59 lines (51 loc) · 2.16 KB
/
main.cpp
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
#include <iostream>
#include <thread>
#include "HttpServer.h"
#include "Configuration.h"
#include "StaticFileController.h"
#include "RestfulAPIController.h"
int main() {
std::cout << "Hello, World!" << std::endl;
std::ifstream ifs("/home/burakon/Projects/per/cervpp/httpserver.json");
Configuration::parseFile(ifs);
HttpServer server;
server.registerController("/staticfile", std::make_shared<StaticFileController>());
auto testRestController = std::make_shared<RestfulAPIController>();
testRestController->route(GET, "/", [] (std::shared_ptr<HttpRequest> req, std::shared_ptr<HttpResponse> resp) {
printf("Route: %s, method: %s\n", req->getURI().c_str(), req->getMethodStr().c_str());
resp->sendJson(R"({"status": "OK"})");
});
testRestController->route(GET, "/auto", [] (auto req, auto resp) {
printf("Route: %s, method: %s\n", req->getURI().c_str(), req->getMethodStr().c_str());
if (!req->getBody().empty()) {
auto jsonBody = req->getJsonBody();
if (jsonBody.isMap()) {
auto json = jsonBody.toMap();
for (auto &item: req->getQueryMap())
json[item.first] = item.second;
resp->sendJson(json);
} else {
resp->sendJson(jsonBody);
}
} else
resp->sendJson(R"({"status": "Auto test"})");
});
testRestController->route(GET, "/mother", [] (std::shared_ptr<HttpRequest> req, std::shared_ptr<HttpResponse> resp) {
printf("Route: %s, method: %s\n", req->getURI().c_str(), req->getMethodStr().c_str());
if (!req->getBody().empty()) {
auto jsonBody = req->getJsonBody();
if (jsonBody.isMap()) {
auto json = jsonBody.toMap();
for (auto &item: req->getQueryMap())
json[item.first] = item.second;
resp->sendJson(json);
} else {
resp->sendJson(jsonBody);
}
} else
resp->sendJson(R"({"status": "Mother send me"})");
});
server.registerController("/test", testRestController);
server.joinThread();
return 0;
}