forked from hasura/imad-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
354 lines (322 loc) · 9.59 KB
/
server.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var port = 80;
var name_list =[]
var count = 0;
var config = {
user:'mithun14leo',
database : 'mithun14leo',
host:'db.imad.hasura-app.io',
port:'5432',
password : process.env.DB_PASSWORD
};
var express = require('express');
var morgan = require('morgan');
var path = require('path');
var Pool = require('pg').Pool;
var crypto = require('crypto');
var bp = require('body-parser');
var app = express();
var pool = new Pool(config);
var map1 = null;
var bat_value = null;
var sensor_value = null;
//
app.use(morgan('combined'));
app.use(bp.json());
// logical
function hash(value,salt){
var return_value = crypto.pbkdf2Sync(value, salt, 10000, 512, 'sha512');
return ['pbkdf','10000', salt, return_value.toString('hex')].join('$');
}
/*
function convert2html(data){
var title = data.title;
var head = data.heading;
var date = data.date;
var content = data.content;
var html_template = `
<html>
<head>
<title>${title}</title>
<link href="/ui/style.css" rel=stylesheet>
<meta name="viewport" content="width: device-width, initial-scale:1">
</head>
<body>
<div>
<a href="/">Home</a>
</div>
<div class="content">
<div>
<h2>
${head}<br>
<hr/>
${date.toDateString()}
</h2>
${content}
</div>
</div>
</body>
</html>
`;
return html_template;
}
*/
//end pionts
function mylocmap(la,lo){
var lat = la;
var lon = lo;
map1 =`<!DOCTYPE html>
<html>
<head>
<title>KayBee</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: ${lat}, lng: ${lon}},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Kaybee is here.' :
'Error: Your browser doesnot support geolocation.');
infoWindow.open(map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBGaEu22Va5yqsYOX_yRQKTrtUyrnorq4w&callback=initMap">
</script>
</body>
</html>
`;
}
app.post ('/battery',function(req,res){
var value = req.body.battery;
bat_value = value;
res.send(200);
});
app.get('/batsts',function(req, res){
if (bat_value !== null)
{
res.send(bat_value.toString());}
else{
res.send("Status Not Updated");
}
});
app.post ('/location',function(req,res){
var lat = req.body.lat;
var lon = req.body.lon;
mylocmap(lat,lon);
res.send(200);
});
app.get('/kaybee',function(req, res){
if (map1 !== null)
{
res.send(map1.toString());}
else{
res.send("Status Not Updated");
}
});
app.post('/upload', function(req, res){
var data = req.body.sd;
pool.query('INSERT INTO sensed_data (data) VALUES ($1)', [data], function(err, result){
if (err){
var err4 = JSON.stringify({'error':err.toString()});
res.send(500).send(err4);
}
else{
var suc = JSON.stringify({'Message': 'Data Uploaded '});
res.send(suc);
}
});
});
app.get('/sen_data',function(req, res){
pool.query('"SELECT data FROM sensed_data"',function(err, result){
if (err){
var error = err.toString();
res.send(500).send(error);
}
else {
res.send(JSON.stringify(result.rows));
}
});
});
app.post('/create-user',function(req,res){
var usna = req.body.username;
var pass = req.body.password;
var salt = crypto.randomBytes(128).toString('hex');
var passq = hash(pass, salt);
pool.query('INSERT INTO all_db (usna, pass) VALUES ($1, $2)', [usna, passq], function(err, result){
if (err){
var err4 = JSON.Stringify({'error':err.toString()});
res.send(500).send(err4);
}
else{
var suc = JSON.stringify({'message': 'Account Successfully created for '+usna});
res.send(suc);
}
});
});
app.post('/login', function(req, res){
var sent_name = req.body.username;
var sent_pass = req.body.password;
pool.query('SELECT * FROM all_db WHERE usna = $1',[sent_name],function(err, result){
if (err){
var error = err.toString();
res.send(500).send(error);
}
else {
if(result.rows.length === 0){
var error2 = JSON.stringify({"error":'Account does not exsist'});
res.send(403).send(error2);
}
else {
var dbs = result.rows[0].pass;
var sal = dbs.split('$')[2];
var has_pas = hash(sent_pass, sal);
if (has_pas === dbs){
var outp = JSON.stringify({"messsage":"Logged in successfully"});
res.send(outp);
}
else {
var error3 = JSON.stringify({"error": 'Incorrect Password'});
res.send(403).write(error3);
}
}
}
});
});
/*app.get('/hash/:value',function(req,res){
var value = req.params.value;
value = hash(value,'hello');
res.send(value);
});*/
/*app.get('/submit-name/:nameid', function (req, res) {
var name = req.params.nameid;
name_list.push(name);
res.send(JSON.stringify(name_list));
});*/
/*app.get('/submit-name', function (req, res) {
var name = req.qurey.nameid;
name_list.push(name);
res.send(JSON.stringify(name_list));
});*/
app.get('/articles/:articletitle', function (req, res) {
pool.query("SELECT * FROM article WHERE title = $1",[req.params.articletitle], function(err, result){
if (err){
res.status(500).send(err.toString());}
else {
if (result.rows.length === 0)
{res.send('Article Not found');}
else {
var articledata = result.rows[0];
res.send(convert2html(articledata));
}
}
});
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'index.html'));
});
app.get('/my',function(req, res){
res.sendFile(path.join(__dirname,'ui','my.html'));
});
app.get('/counter',function(req, res){
count = count + 1;
res.send(count.toString());
});
app.get('/submitname', function (req, res) {
var name = req.query.name;
name_list.push(name);
res.send(JSON.stringify(name_list));
});
app.get('/dummy2', function (req, res) {
var name = req.query.name;
pool.query("SELECT * FROM dummy WHERE title != $1",[name], function(err, result){
if (err){
res.status(500).send(err.toString());}
else {
if (result.rows.length === 0)
{res.send('Article Not found');}
else {
var articledata = result.rows;
res.send(JSON.stringify(articledata));
}
}
});
});
app.get('/ui/style.css', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'style.css'));
});
app.get('/ui/madi.png', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
});
app.get('/ui/a4img.png', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'a4img.png'));
});
app.get('/ui/main.js', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'main.js'));
});
app.get('/ui/silo.js', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'silo.js'));
});
app.get('/signup_login', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'silo.html'));
});
app.get('/ui/dummy.html', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'dummy.html'));
});
app.get('/ui/dummy.js', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'dummy.js'));
});
app.get('/sen', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'sen_dat.html'));
});
app.get('/ui/list.js', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'list.js'));
});
app.listen(port, function () {
console.log(`IMAD course app listening on port ${port}!`);
});