This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (81 loc) · 2.5 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
const https = require('https');
const express = require('express');
const normalizePort = require('normalize-port');
var app = express();
const JSON = require('JSON');
/** database init */
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./chinook.db', (err)=>{
if (err){
return console.error(err.message);
}
// console.log('wegucci')
});
/** end database init */
app.use(express.static('public'));
/** api routes */
app.post('/api/enable', (req,res)=>{
let query = `UPDATE take_me_to SET enabled = ?`;
let data = ['true']
db.run(query, data, (err)=>{
if (err){
return console.error(err.message);
}
// console.log(`enabled location view`);
});
res.sendStatus(200);
});
app.post('/api/disable', (req,res)=>{
let query = `UPDATE take_me_to SET enabled = ?, latitude = ?, longitude = ?`;
let data = ['false', '', '']
db.run(query, data, (err)=>{
if (err){
return console.error(err.message);
}
// console.log(`disabled location view`);
});
res.sendStatus(200);
});
app.get('/api/fetch', (req, res)=>{
let query = `SELECT * FROM take_me_to`
db.get(query, (err, row)=>{
if(err){
return console.error(err.message);
}
// console.log(JSON.stringify(row))
res.send(row)
});
});
const httpGet = (url) => {
return new Promise((resolve, reject) => {
https.get(url, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => resolve(body));
}).on('error', reject);
});
};
app.put('/api/new-place/:location', async (req, res)=>{
var location = req.params.location;
var body = await httpGet(`https://maps.googleapis.com/maps/api/geocode/json?address=${location}&key=AIzaSyDREz7hHVEKem4yrgn6P7FOhJJO1mAwbVE`)
var formattedData = JSON.parse(body);
// var locationPair = {
// latitude: null,
// longitude: null
// }
let query = `UPDATE take_me_to SET latitude = ?, longitude = ?`;
let queryData = [formattedData.results[0].geometry.location.lat, formattedData.results[0].geometry.location.lng]
db.run(query, queryData, (err)=>{
if (err){
console.error(err)
}
res.sendStatus(200)
});
// console.log(location)
// console.log(body)
})
/** end api routes */
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
app.listen(port);