-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (60 loc) · 1.99 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
'use strict';
const express = require('express');
const radioClient = require('./radio-blaster');
const firebaseClient = require('firebase-admin');
const firebaseServiceAccount = require('./firebase-service-account.json');
const bodyParser = require('body-parser');
const fcmTopic = process.env.RADIO_STATION_KEY;
const radioName = process.env.RADIO_STATION_NAME;
/** Initialize firebase API client. */
firebaseClient.initializeApp({
credential: firebaseClient.credential.cert(firebaseServiceAccount)
});
/** Start parsing of radio stream. */
radioClient.start();
radioClient.onTrackUpdate(metadata => {
console.log('Publishing track metadata:', metadata);
var trackUpdateMessage = {
data: metadata,
topic: fcmTopic,
android: {
ttl: 0,
priority: 'high'
}
};
// Send a track metadata to devices subscribed to the FCM topic.
firebaseClient
.messaging()
.send(trackUpdateMessage)
.then((response) => {
// Response is a message ID string.
console.log('Successfully published message:', response);
})
.catch((error) => {
console.log('Error publishing to topic:', error);
});
});
/** Create the Express application. */
const app = express();
app.set('trust proxy', true);
app.use(bodyParser.json()); // support json bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use('/', require('./routes')(firebaseClient, radioClient, fcmTopic));
/** Express' final error handler. */
app.use(function(error, req, res, next) {
if (res.headersSent) {
return next(error);
}
if(error.code && error.code >= 99 && error.code <= 599) {
return res.status(error.code).send(error);
} else {
return next(error);
}
});
// Listen to the App Engine-specified port, or 3000 otherwise
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
console.log(`Parsing metadata from ${radioClient.getStreamUrl()}...`);
});