-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.cpp
executable file
·242 lines (200 loc) · 6.65 KB
/
serve.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
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
#ifdef _MSC_VER
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <map>
#include <mongoose/Server.h>
#include <mongoose/WebController.h>
#include <pqxx/pqxx>
#include "databaseQueries.cpp"
string TWILIO_NUM = "+17572315125";
using namespace std;
using namespace Mongoose;
using namespace pqxx;
string strcast(char* str_) {
string str = str_;
return str;
}
class MyController : public WebController {
public:
void twilio(Request &request, StreamResponse &response) {
//cerr << "URL: " << request.getUrl() << "\n";
//cerr << "Method: " << request.getMethod() << "\n";
//cerr << "Data: " << request.getData() << "\n";
map<string, string> myMap = request.getAllVariable();
for(auto elem : myMap) {
cerr << elem.first << " " << elem.second << "\n";
}
try {
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
response << form_sms_response("+1" + myMap["From"].erase(0,3), "Test successful: " + myMap["Body"]);
}
void http(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
string r = "";
for(auto elem : myMap) {
r += "\n" + elem.first + ": " + elem.second;
cerr << elem.first << ": " << elem.second << "\n";
}
try {
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
response << r;
}
string form_sms_response(string to, string message) {
string sms = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
sms += "<Response>";
sms += "<Sms from=\"" + TWILIO_NUM + "\" to=\"" + to + "\">" + message + "</Sms>";
sms += "</Response>";
return sms;
}
void send_sms(string phone_number, string msg) {
/*
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/{Account SID}/Messages.xml' \
> --data-urlencode 'To=+17579683627' \
> --data-urlencode 'From=+17572315125' \
> --data-urlencode 'Body=This is a curl test.' \
> -u {Account SID}:25c4d5dbc8f0f8a6cb37c39156ce97cf
*/
}
void send_im() {
}
void send_message(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
for(auto elem : myMap) {
cerr << elem.first << " " << elem.second << "\n";
}
//getGroup
//postMessage
}
void post_message(double group_id, string msg) {
//database INSERT
send_message_to_members(group_id, msg);
}
void send_message_to_members(double group_id, string msg) {
//getGroup
//for all users in group
//send message
}
void authenticate(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
try {
//TODO: If user exists, return id
}
catch(const std::exception &e) {
//TODO: If not, create user and return id
}
}
void get_all_groups(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
std::string::size_type sz; // alias of size_t
double user_id = std::stod (myMap["user_id"], &sz);
try {
pqxx::result r = Query::getGroupsFromUser(user_id);
response << r.query();
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
}
void get_user(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
std::string::size_type sz; // alias of size_t
double user_id = std::stod (myMap["user_id"], &sz);
try {
pqxx::result r = Query::getUser(user_id);
response << r.query();
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
}
void get_group(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
std::string::size_type sz; // alias of size_t
double group_id = std::stod (myMap["group_id"], &sz);
try {
pqxx::result r = Query::getGroupInfo(group_id);
response << r.query();
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
}
void create_group(Request &request, StreamResponse &response) {
map<string, string> myMap = request.getAllVariable();
try {
Query::createGroup(myMap["group_name"]);
//TODO: What is the group's id?
response << myMap["group_name"];
}
catch(const std::exception &e) {
response << "ERROR: " << e.what();
}
}
void setup() {
addRoute("POST", "/twilio", MyController, twilio);
addRoute("POST", "/http", MyController, http);
addRoute("POST", "/send_message", MyController, send_message);
addRoute("POST", "/authenticate", MyController, authenticate);
addRoute("POST", "/get_all_groups", MyController, get_all_groups);
addRoute("POST", "/get_user", MyController, get_user);
addRoute("POST", "/get_group", MyController, get_group);
addRoute("POST", "/create_group", MyController, create_group);
/*
Authentication - if successful, return user object with id, email, name
Get all groups - return array of all those fields in the database
Get a user given user id
Get group given group id
Get all messages given a group
Create a group
*/
}
};
int main() {
try {
//host=pdc-amd01.poly.edu dbname=sw2371 user=jsw407 password=ecrnpazb
connection C("host=" + strcast(getenv("pghost")) + " dbname=" + strcast(getenv("pgdbname")) + " user=" + strcast(getenv("pguser")) + " password=" + strcast(getenv("pgpassword")));
cout << "Connected to " << C.dbname() << endl;
/*
work W(C);
result R = W.exec("SELECT name FROM employee");
cout << "Found " << R.size() << "employees:" << endl;
for (result::const_iterator r = R.begin();
r != R.end();
++r)
{
cout << r[0].c_str() << endl;
}
cout << "Doubling all employees' salaries..." << endl;
W.exec("UPDATE employee SET salary=salary*2");
cout << "Making changes definite: ";
W.commit();
cout << "ok." << endl;
*/
}
catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
MyController myController;
Server server(8080);
server.registerController(&myController);
server.start();
while (1) {
#ifdef WIN32
Sleep(10000);
#else
sleep(10);
#endif
}
}