-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (92 loc) · 2.8 KB
/
app.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
// require('dotenv').config()
const http = require('http')
const env = require('cfenv').getAppEnv()
const path = require('path')
const express = require('express')
const app = express()
const request = require('request')
const simulator = require('./simulator.js')
const server = http.createServer((req,res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
res.end();
return;
}
})
// weather_url should be format of https://username:[email protected]
// const weatherURL = process.env['simulator_weather_url'];
const weatherAPIKey = process.env['simulator_weather_api_key'];
const weatherURL = ''
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'))
})
app.get('/start', (req, res) => {
simulator.start(null, server)
.then(() => {
res.send('Started simulator')
})
.catch(err => {
console.error(err)
res.status(500).send(`Error starting Simulator: ${err.message || err}`)
})
})
app.get('/stop', (req, res) => {
simulator.stop()
.then(() => {
res.send('Stopped simulator')
})
.catch(err => {
console.error(err)
res.status(500).send(`Error stopping Simulator: ${err.message || err}`)
})
})
app.get('/pause', (req, res) => {
simulator.pause()
.then(() => {
res.send('Paused simulator')
})
.catch(err => {
console.error(err)
res.status(500).send(`Error pausing Simulator: ${err.message || err}`)
})
})
app.get('/continue', (req, res) => {
simulator.continue()
.then(() => {
res.send('Continued simulator')
})
.catch(err => {
console.error(err)
res.status(500).send(`Error continuing Simulator: ${err.message || err}`)
})
})
app.get('/info', (req, res) => {
simulator.info()
.then(info => {
res.send(info)
})
.catch(err => {
console.error(err)
res.status(500).send(`Error getting Simulator info: ${err.message || err}`)
})
})
app.get('/weather/:lat/:lon', (req, res) => {
if (weatherAPIKey) {
const url = `https://api.weather.com/v1/geocode/${req.params.lat}/${req.params.lon}/observations.json?units=e&language=en-US&apiKey=` + weatherAPIKey
// const url = weatherURL + `/api/weather/v1/geocode/${req.params.lat}/${req.params.lon}/observations.json?units=e&language=en-US`
request(url, (err, response, body) => {
console.log(body)
res.send(body)
})
} else {
res.status(500).send('Weather API Key not configured')
}
})
server.on('request', app)
server.listen(env.port, () => {
console.log(`To view your app, open this link in your browser: ${env.url}`)
})