-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
190 lines (156 loc) · 4.91 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
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
#include <iostream>
#include <cassert>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <cpprest/json.h>
#include <cpprest/http_listener.h>
#include "serverListManager.h"
using namespace std;
bool runserver = true;
void sig_handler (int signum)
{
runserver = false;
}
//UTILITY FUNCTIONS
bool readFromFile(std::string myfile, web::json::value& res_json)
{
try
{
std::ifstream json_file(myfile, std::ifstream::in);
std::stringstream filestream;
filestream << json_file.rdbuf();
res_json = web::json::value::parse(filestream);
json_file.close();
}
catch (web::json::json_exception excep)
{
cout << excep.what() << endl;
return false;
}
return true;
}
bool fillRequestList(serverListManager* request,web::json::value * res_json)
{
web::json::array servers = res_json->as_array();
web::json::value server;
std::string method;
std::string endpoint;
std::string body;
std::map<std::string,std::string> headers;
for(web::json::array::iterator it = servers.begin();it != servers.end();it++)
{
server = (*it).at("request");
method = server.at("method").as_string();
endpoint = server.at("endpoint").as_string();
if(server.has_field("body"))
body = server.at("body").as_string();
else
body = "";
if(server.has_field("headers"))
{
headers.clear();
web::json::array jsonheaders = server.at("headers").as_array();
std::string key;
std::string value;
for ( std::vector<web::json::value>::iterator ith = jsonheaders.begin(); ith < jsonheaders.end();ith++)
{
key = (*ith).at("key").as_string();
value = (*ith).at("value").as_string();
headers.insert(std::pair<std::string,std::string>(key,value));
}
}
serverRequest* newreq = new serverRequest(method, endpoint, headers,body);
if( (*it).has_field("description"))
newreq->setDescription((*it).at("description").as_string());
if((*it).has_field("response_ok"))
{
newreq->setValidator((*it));
}
request->addtoList(newreq);
}
return true;
}
void handle_live(web::http::http_request request)
{
cout << "gets the call....:" << endl;
request.reply(200, "ok");
}
void* setListener(void *)
{
web::http::experimental::listener::http_listener listener("http://0.0.0.0:3000/healthz");
listener.support("GET", handle_live);
listener.support("POST", handle_live);
listener.support("PUT", handle_live);
listener.support("DEL", handle_live);
try
{
listener.open()
.then([&listener]() {cout << "starting server..." << endl;})
.wait();
while (runserver);
}
catch (exception const & e)
{
cout << "Unable to set listener....." << endl;
}
}
int main(int argc, char **argv)
{
if(argc != 2)
{
cout << "Usage:" << endl;
cout << " " << argv[0] << "configurationfile.json" << endl;
return 0;
}
//Handlers to stop the server
signal (SIGINT, sig_handler);
signal (SIGHUP, sig_handler);
signal (SIGTERM, sig_handler);
web::json::value configjson;
// Configuration read
if(!readFromFile(argv[1], configjson))
{
std::cerr << "unable to run manager, cant read file" << argv[1] << std::endl;
return false;
}
serverListManager listmanager;
if(!fillRequestList(&listmanager, &configjson) || listmanager.size() == 0)
{
std::cerr << "unable to check list manager, cant read request list or empty" << std::endl;
return false;
}
//We build the notificator to send data to our server
std::map<std::string,std::string> notiheaders;
notiheaders.insert(std::pair<std::string,std::string>("Authorization", "Bearer 38f51854b1a282d8e9acdb74710fccc0ba3eb4db"));
serverRequest notificator("POST", "https://interview-notifier-svc.spotahome.net/api/v1/notification", notiheaders);
// serverRequest notificator("GET", "https://interview-notifier-svc.spotahome.net/api/v1/notification");
//set listener
pthread_t listener_thread;
if(pthread_create(&listener_thread, NULL, setListener, NULL))
{
cout << "Unable to set listener....." << endl;
}
//Now run until a signal stop us....
while(runserver)
{
serverRequest* nextrequest = listmanager.getNext();
if(!nextrequest->call())
{
std::string send_info = "{\"service\":\"";
send_info.append(nextrequest->endpoint());
send_info.append("\",\"description\":\"");
send_info.append(nextrequest->description());
send_info.append("\"}");
if(!notificator.call(send_info))
cout << "WE HAVE NOT CONNECTION WITH THE SERVER. Data not sended:" << send_info << endl;
} else
cout << "SUCCESS:" << nextrequest->endpoint() << endl;
sleep(2); //We wait between calls....
}
return 0;
}