-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
203 lines (162 loc) · 7.27 KB
/
main.cpp
File metadata and controls
203 lines (162 loc) · 7.27 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
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string>
#include "../include/Server.h"
#include "../include/WebController.h"
#include "../include/JsonController.h"
#include "../include/JsonResponse.h"
#include "../include/json.h"
#include "../include/mysql++.h"
#include "../include/StreamResponse.h"
#include "../include/options.h"
#include "../include/row.h"
//#include <tchar.h>
using namespace std;
using namespace Mongoose;
using namespace mysqlpp;
class MyController : public WebController
{
public:
void hello(Request &request, StreamResponse &response)
{
// response << "Hello " << htmlEntities(request.get("username", "... what's your name ?")) << endl;
/* string username = request.get("username");
string passwd = request.get("passwd");
string body = response.getBody();
string data = request.getData();
cout << "username = " << username << endl;
cout << "passwd = " << passwd << endl;
cout << "request method = " << request.getMethod() << endl;
if (username == "yhj" && passwd == "123") {
response << "{\"msg\":\"Congratulations,Login success\"}" << endl;
} else {
response << "{\"msg\":\"Sorry,wrong username or password\"}" << endl;
}
response << "body = " << body << endl;
cout << "body = " << body << endl;
response << "data = " << data << endl;
cout << "data = " << data << endl;*/
/* string body = request.get("body","no body contaent");
cout << "body = " << body << endl;
response << body << endl;*/
string body = request.getData();
Json::Reader reader;
Json::Value value;
if (reader.parse(body,value)) {
string username = value["username"].asString();
string passwd = value["passwd"].asString();
cout << "Username = " << username << endl;
cout << "Password = " << passwd << endl;
if (username == "yhj" && passwd == "123") {
response << "Congratulations,Login OK" << endl;
} else {
response << "Sorry,Login fail,wrong name or passwd!!!" << endl;
}
}
};
void reg(Request &request,StreamResponse &response) {
string body = request.getData();
Json::Reader reader;
Json::Value value;
if (reader.parse(body,value)) {
string username = value["username"].asString();
string passwd = value["passwd"].asString();
cout << "注册用户名: " << username << endl;
cout << "注册密码: " << passwd << endl;
mysqlpp::Connection conn = mysqlpp::Connection(false);
conn.set_option(new SetCharsetNameOption("utf8"));
conn.connect("cpp","127.0.0.1","root","root");
// conn.set_option(new SetCharsetNameOption("utf8"));
mysqlpp::Query query = conn.query();
/*string sql1 = ToUTF8(L("select * from t_user where username='" + username + "'"));
string sql2 = ToUTF8(L("insert into t_user(username,passwd) values('" + username + "','" + passwd + "')")); */
string sql1 = "select * from t_user where username='" + username + "'";
string sql2 = "insert into t_user(username,passwd) values('" + username + "','" + passwd + "')";
// print_stock_table(query);
// mysqlpp::Transaction trans(conn,mysqlpp::Transaction::serializable,mysqlpp::Transaction::session);
// mysqlpp::SimpleResult result = query.execute(sql1);
// mysqlpp::ulonglong r = result.rows();
query << sql1 ;
mysqlpp::StoreQueryResult result = query.store();
mysqlpp::StoreQueryResult::list_type::size_type r = result.num_rows();
cout << "sql1 = " << sql1 << endl;
cout << "sql2 = " << sql2 << endl;
cout << "r = " << r << endl;
if (r > 0) {
response << "1" << endl;
} else {
if(query.execute(sql2).rows() > 0) {
response << "0" << endl;
} else {
response << "2" << endl;
}
}
cout << "query = " << query << endl;
} else {
response << "2" << endl;
}
};
void login(Request &request,StreamResponse &response) {
string body = request.getData();
cout << "body = " << body << endl;
Json::Reader reader;
Json::Value value;
if (reader.parse(body,value)) {
string username = value["username"].asString();
string passwd = value["passwd"].asString();
cout << "username = " << username << endl;
cout << "passwd = " << passwd << endl;
Connection conn = Connection(false);
conn.set_option(new SetCharsetNameOption("utf8"));
conn.connect("cpp","127.0.0.1","root","root");
string sql = "select count(*) from t_user where username = '" + username + "' and passwd = '" + passwd + "'";
cout << "sql = " << sql << endl;
Query query = conn.query();
query << sql;
StoreQueryResult result = query.store();
//mysqlpp::StoreQueryResult::list_type::size_type r = result.num_rows();
// Row row = result.fetch_row();
// Row::const_reference count = row.at(0);
//cout << "count = " << count << endl;
//string field_name = result.field_name(0);
//cout << "field_name = " << field_name << endl;
// Row row = result.fetch_row();
// cout << row.begin() << endl;
StoreQueryResult::const_iterator it = result.begin();
Row row = *it;
if (it != result.end()) {
if (row[0]) {
response << "0" << endl;
} else {
response << "1" << endl;
}
//cout << "cout = " << row[0] << endl;
}
// cout << "count = " << *it[0] << endl;
}
}
void setup()
{
// addRoute("GET", "/hello", MyController, hello);
addRoute("POST","/hello",MyController,hello);
addRoute("POST","/reg",MyController,reg);
addRoute("POST","/login",MyController,login);
}
/* std::string ToUTF8(const wchar_t* wideStr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(wideStr);
}*/
};
int main()
{
MyController myController;
Server server(8080);
server.registerController(&myController);
server.start();
cout << "Server has started,listening port is 8080 " << endl;
while (1) {
sleep(10);
}
}