-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (107 loc) · 2.38 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var Hapi = require('hapi');
var Feed = require('./models/feed');
var Subscriptions = require('./models/subscriptions');
var async = require('async');
var options = {
views: {
path: 'templates',
engines: {
hbs: 'handlebars'
}
}
};
// Create a server with a host and port
var server = Hapi.createServer('0.0.0.0', parseInt(process.env.PORT, 10) || 3000, options);
var subscriptions = new Subscriptions();
var removeDuplicates = function(unfiltered, callback) {
// Currently checks for episodes. Should also check for dupes
if (callback) {
if (unfiltered instanceof Array) {
callback(true);
return;
}
callback(false);
}
};
var index = {
handler: function (req, reply) {
// Render the view
reply.view('index.hbs', {});
}
};
var add = {
handler: function(req, reply) {
var payload = req.payload;
for (var item in payload) {
if (typeof payload !== 'string' && payload.hasOwnProperty(item)) {
var temp_str = req.payload[item] ? '=' + req.payload[item] : '';
payload = item + temp_str;
}
}
var feed = new Feed(payload);
async.series([
function(callback) {
feed.fetch(callback);
},
function(callback) {
subscriptions.addShow(feed, callback);
}
],
function(err, results){
async.filter(results, removeDuplicates,
function(results) {
reply({subscriptions: results[0]});
}
);
});
}
};
var remove = {
handler: function(req, reply) {
async.series([
function(callback) {
subscriptions.removeShow(req.payload, callback);
}
],
function(err, results){
async.filter(results, removeDuplicates,
function(results) {
reply({subscriptions: results[0]});
}
);
});
}
};
// Add the route
server.route({
method: 'GET',
path: '/',
config: index
});
server.route({
method: 'POST',
path: '/add',
config: add
});
server.route({
method: 'DELETE',
path: '/remove',
config: remove
});
// Serve static resources
server.route({
method: 'GET',
path: '/public/{path*}',
handler: {
directory: { path: './public', listing: false, index: true }
}
});
server.route({
method: 'GET',
path: '/css/{path*}',
handler: {
directory: { path: './css', listing: false, index: true }
}
});
// Start the server
server.start();