-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAPI.cpp
More file actions
129 lines (108 loc) · 3.34 KB
/
TestAPI.cpp
File metadata and controls
129 lines (108 loc) · 3.34 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
#define _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <iostream>
#include "BLC.h"
#include "API_Logger.h"
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
using namespace web::json;
auto logger = new API_Logger();
int main()
{
uri_builder uri(U("http://localhost:8080"));
auto addr = uri.to_uri().to_string();
http_listener listener(addr);
//BLC blc;
//blc.TestBLC();
auto cors_handler = [](http_request request)
{
auto method = request.method();
if (method == methods::OPTIONS)
{
http_response response(status_codes::OK);
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
request.reply(response);
return;
}
request.headers().add(U("Access-Control-Allow-Origin"), U("*"));
};
listener.support(methods::GET, [](http_request request) {
http_response resp;
resp.headers().add(U("Access-Control-Allow-Origin"), U("*"));
auto path = request.relative_uri().path();
try
{
if (path == U("/")) {
logger->LogInfo("GET \"/\"");
resp.set_body(json::value::object({ {U("message"), json::value::string(U("Hi There! We're C++, looking forward to working with ya'all!!!"))} }));
resp.set_status_code(status_codes::OK);
}
else if (path == U("/ping")) {
logger->LogInfo("GET \"/ping\"");
resp.set_body(json::value::object({ {U("message"), json::value::string(U("Pong"))} }));
resp.set_status_code(status_codes::OK);
}
else {
logger->LogInfo("GET \"Not Found\"");
resp.set_status_code(status_codes::NotFound);
resp.set_body(U("Not Found"));
}
}
catch (const std::exception&)
{
logger->LogError("Exception occurred while processing GET request.");
}
request.reply(resp);
});
listener.support(methods::OPTIONS, [](http_request request) {
http_response response(status_codes::OK);
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
request.reply(response);
});
listener.support(methods::POST, [cors_handler](http_request request)
{
request.extract_json().then([=](pplx::task<json::value> task)
{
try
{
auto jsonData = task.get();
if (jsonData.has_field(U("name")))
{
auto name = jsonData[U("name")].as_string();
json::value response;
response[U("message")] = json::value::string(U("Hello, ") + name + U("!"));
request.reply(status_codes::OK, response);
}
else
{
request.reply(status_codes::BadRequest, U("Missing 'name' field"));
}
}
catch (http_exception& e)
{
request.reply(status_codes::BadRequest, U("Invalid JSON"));
logger->LogError(e.what());
}
});
});
try
{
listener.open().wait();
logger->LogInfo("Server started and listening on port 8080");
logger->LogInfo("Press Enter to stop the server");
std::string line;
std::getline(std::cin, line);
listener.close().wait();
}
catch (std::exception& e)
{
logger->LogError(e.what());
}
return 0;
}