-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (77 loc) · 2.64 KB
/
app.js
File metadata and controls
93 lines (77 loc) · 2.64 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
// ==================== REQUIRE STATEMENTS ====================
require('dotenv').config()
const CONFIG = require('./config')
const EXPRESS = require('express')
const APP = EXPRESS()
const SERVER = require('http').createServer(APP)
const PATH = require('path')
const IO = require('socket.io')(SERVER)
const REQUEST = require('request')
const GPS = require('./modules/gps')
const PLAYLISTS = require('./modules/music').PLAYLISTS
const MUSIC = require('./modules/music').MUSIC
const WEATHER = require('./modules/weather')
// ==================== MISC SETUP ====================
APP.use('/assets/', EXPRESS.static(PATH.join(__dirname, '/public')))
APP.use('/', EXPRESS.static(__dirname))
// ==================== SETUP FOR GPSD DAEMON ====================
if (process.env.ENVIRONMENT !== 'dev') {
GPS.DAEMON.start(GPS.daemonInit)
}
// ==================== SETUP FOR GOOGLE MUSIC ====================
PLAYLISTS.getData()
// ==================== SETUP FOR SOCKET.IO ====================
// expose socket to GPS and WEATHER so we can emit data from there as it arrives rather than running loops here
function exposeSocket (socket) {
GPS.currentSocket = socket
WEATHER.currentSocket = socket
}
IO.on('connection', function (socket) {
console.log('Socket.io running')
exposeSocket(socket)
WEATHER.startWeatherLoop()
})
// ==================== ROUTES ====================
// Send Index page
APP.get('/', function (req, res) {
res.sendFile('./index.html')
})
// ==================== GOOGLE PLAY MUSIC ROUTES ====================
// client makes request to /music, gets back obj with playlists and all their songs
APP.get('/music', function (req, res) {
if (!PLAYLISTS.playlists.names) {
PLAYLISTS.getData()
}
res.send(JSON.stringify(PLAYLISTS.playlists))
})
// client requests song stream by storeId
APP.get('/music/:song', function (req, res) {
MUSIC.getStream(req.params.song, function (err, stream) {
if (err) {
console.log(err)
} else {
stream.pipe(res)
}
})
})
// When user clicks music reset button, re-connect to Google server, then reload index page
APP.get('/reset-music', function (req, res) {
PLAYLISTS.getData()
res.redirect('/')
})
// ==================== GOOGLE MAPS API ROUTE ====================
APP.get('/private/apikey/', function (req, res) {
let pageres = res
// Keep the API key private.
REQUEST(process.env.API_KEY, function (err, res, body) {
if (err) {
console.log(err)
} else if (res.statusCode === 200) {
pageres.send(body)
}
})
})
// ==================== LISTEN ON PORT 8000 ====================
SERVER.listen(CONFIG.port, function () {
console.log('Rav4Pi server is running!')
})