-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (47 loc) · 1.33 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
'use strict';
var express = require('express'),
http = require('http'),
bodyParser = require ('body-parser'),
morgan = require ('morgan'),
faye = require('faye');
var PollCurrentSong = require('./utilities/current_song.js');
var cfg = require('./radiogaga.json');
/**/
var app = express();
/* default configurations */
app.set('view engine', 'jade')
app.set('views', __dirname + '/src');
app.engine('html', require('ejs').renderFile);
app.use(morgan());
app.use(bodyParser());
app.use(express.static(__dirname + '/public'));
var bayeux = new faye.NodeAdapter({
mount: '/faye',
timeout: 45
});
var server = http.createServer(app);
bayeux.attach(server);
/* routes */
app.get('/', function(req, res) {
res.render('index.jade');
});
app.get('/:channel', function(req, res) {
var _channel = {
station : null,
imgSrc : null
};
var found = cfg.channels.some(function(c) {
if (c.channel === req.params.channel) {
_channel.station = cfg.api.station + c.mount,
_channel.imgSrc = c.img
return true;
}
});
var _render = found ? 'channel.jade' : 'not_found.jade';
res.render('../src/' + _render, _channel);
});
var _pollCurrentSong = new PollCurrentSong();
_pollCurrentSong.addEventListener(function(data) {
bayeux.getClient().publish('/channel', data);
});
module.exports = app;