-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·58 lines (50 loc) · 1.74 KB
/
index.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
const config = require('./config.json');
const player = require('./lib/player.js');
function isAuthenticated(req, res) {
if (req.body.secret && req.body.secret === config.SECRET) {
return true;
}
console.error('Authentication failed for client');
res.status(401).end();
return false;
}
function handleRequest(handler, req, res) {
// Ensure authentication
if (!isAuthenticated(req, res)) return;
handler()
.then(res.end())
.catch((body) => {
console.error(body);
res.status(500).end();
});
}
function playOnDevice(req) {
let device;
let play;
if (req.get('content-type') === 'application/json') {
({ device, play } = req.body);
}
// Start playback on device
return player.playOnDevice(device, play);
}
function setVolume(req) {
if (!req.get('content-type') === 'application/json') {
throw new Error('Content-type must be JSON');
}
const { volume, device } = req.body;
return player.setVolume(volume, device);
}
function adjustVolume(req) {
if (!req.get('content-type') === 'application/json') {
throw new Error('Content-type must be JSON');
}
const { volumeChange } = req.body;
return player.adjustVolume(volumeChange);
}
exports.skipNext = (req, res) => handleRequest(player.skipNext, req, res);
exports.skipPrevious = (req, res) => handleRequest(player.skipPrevious, req, res);
exports.pause = (req, res) => handleRequest(player.pause, req, res);
exports.play = (req, res) => handleRequest(player.play, req, res);
exports.playOnDevice = (req, res) => handleRequest(playOnDevice.bind(this, req), req, res);
exports.setVolume = (req, res) => handleRequest(setVolume.bind(this, req), req, res);
exports.adjustVolume = (req, res) => handleRequest(adjustVolume.bind(this, req), req, res);