-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-http.js
executable file
·230 lines (178 loc) · 6.48 KB
/
main-http.js
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
#!/usr/bin/env node
const express = require("express"),
cors = require("cors"),
path = require("path"),
mysql = require("mysql"),
fs = require("fs"),
http = require("http"),
bcrypt = require('bcryptjs');
config = require('./config');
configMdp = require('../config-passwd/config');
const app = express();
const saltRounds = 10; //this variable is used to encrypt the password
var log = function (msg) {
if (config.log) {
console.log(msg);
}
}
log("[Info] : Logging enabled");
app.use(cors());
var basepath = path.resolve(__dirname);
var whitelist = config.origins;
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
optionsSuccessStatus: 200
}
var port = config.port;
log("[Info] App listenning request from " + config.origins + " on port :" + port);
app.options('/', cors(corsOptions)); // enable pre-flight request for OPTIONS request
app.get('/', cors(corsOptions), function (req, res) {
res.send("nothing to see here");
});
app.post('/', cors(corsOptions), function (req, res) {
res.end("OK");
});
app.options('/data', cors(corsOptions)); // enable pre-flight request for OPTIONS request
app.options('/createUser', cors(corsOptions)); // enable pre-flight request for OPTIONS request
app.options('/signInUser', cors(corsOptions));
app.listen(port);
var connection;
var db_code = [],
db_lib_organe = [],
db_lib_lesion = [],
db_rapport = [],
db_emplacement = [];
init();
app.get('/data', cors(corsOptions), function (req, res) {
sqlquery(req.query.keyword, function (rows) {
//console.log("return from DB = " + rows);
if (rows == false) {
res.send({
error: true
})
} else {
//ce tableau permet de faire passer l'ensemble des rapports qui sont liés à un seul code.
//il se peut que un code soit lié à plusieur rapports.
for (var index = 0; index < rows.length; index++) {
db_code.push(rows[index].num_exam);
db_lib_organe.push(rows[index].lib_organe);
db_lib_lesion.push(rows[index].lib_lesion);
db_rapport.push(rows[index].rapport);
db_emplacement.push(rows[index].emplacement);
}
res.send({
code: db_code,
lib_organe: db_lib_organe,
lib_lesion: db_lib_lesion,
rapport: db_rapport,
emplacement: db_emplacement,
error: false
});
db_code = [], db_lib_organe = [], db_lib_lesion = [], db_rapport = [], db_emplacement = [];
}
});
});
function init() {
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: configMdp.passwd,
database: 'pathoSearch',
socketPath: '/var/run/mysqld/mysqld.sock'
});
connection.connect(function (err) {
if (err) {
log('[Error] : Error connecting to DB: ' + err.stack);
return;
}
log('[INFO] : Connected to DB as id ' + connection.threadId);
});
}
function sqlquery(keyword, callback) {
var query_db = "SELECT `num_exam`, `lib_organe`, `lib_lesion`, `rapport`, `emplacement` FROM `database` WHERE `num_exam`='" + keyword + "'";
connection.query(query_db, function (err, rows) {
if (err) {
return callback(false);
}
return callback(rows);
});
}
/************************** USER SIGNUP ***************************/
app.get('/createUser', cors(corsOptions), function (req, res) {
sqlcreateUser(req.query.Name, req.query.FirstName, req.query.password, req.query.UserEmail, req.query.profilePicture, function (rows) {
if (rows == false) {
res.send({
userCreatorError: true,
userCreatormessage: "Une erreur c'est produite lors de la création de votre compte"
})
}
else {
res.send({
userCreatorError: false,
userCreatormessage: "Votre compte a été crée"
})
}
});
});
//TODO add the picture in the query
function sqlcreateUser(name, firstName, password, email, profilePicture, callback) {
checkPresenceUser(name, firstName, function (rows) {
if (rows == false) {
bcrypt.hash(password, saltRounds, function (err, hash) {
var query_db = "INSERT INTO `employees`(`name`, `first_name`, `password`, `profilePicture`, `email`) VALUES ('" + name + "','" + firstName + "','" + hash + "','" + profilePicture + "','" + email + "')";
connection.query(query_db, function (err, result) {
if (err) throw err;
return callback(result);
});
});
}
else {
return callback(false);
}
});
}
function checkPresenceUser(name, first_name, callback) {
var query_db = "SELECT `name` FROM `employees` WHERE `name` ='" + name + "' AND `first_name` = '" + first_name + "'";
connection.query(query_db, function (err, result) {
if (err) throw err;
return callback(result);
});
}
/************************** USER SIGNIN ***************************/
app.get('/signInUser', cors(corsOptions), function (req, res) {
sqlSignInUser(req.query.Name, req.query.FirstName, req.query.password, function (rows) {
if (rows == false) {
res.send({
userSignInError: true,
userSignInMessage: "Une erreur c'est produite lors de la connexion"
})
}
else {
res.send({
userSignInError: false,
userSignInMessage: "Connecté"
})
}
});
});
function sqlSignInUser(name, firstName, userPassword, callback) {
var query_db = "SELECT `password` FROM `employees` WHERE `name` ='" + name + "' AND `first_name` = '" + firstName + "'";
connection.query(query_db, function (err, resultDB) {
if (err) throw err;
if (resultDB[0].password) {
bcrypt.compare(userPassword, resultDB[0].password, function (err, result) {
if (err) throw err;
if (result == true) {
return callback(true);
}
});
}
else return callback(false);
});
}