forked from ERNICommunity/at11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (100 loc) · 3.27 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
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
var express = require('express');
var hbs = require('hbs');
var moment = require("moment-timezone");
//our modules
var config = require('./config');
var menuFetcher = require('./menuFetcher');
var parserUtil = require('./parsers/parserUtil');
console.log("Initializing...");
var actions = {};
function createAction(url, postParams, parseCallback) {
return function(date, doneCallback) {
menuFetcher.fetchMenu(url, date, postParams, parseCallback, doneCallback);
};
}
for (var i = 0; i < config.restaurants.length; i++)
{
console.log(config.restaurants[i]);
try
{
var parserModule = require("./parsers/" + config.restaurants[i].module);
if (typeof parserModule.parse !== "function")
{
throw "Module is missing parse method";
}
if (parserModule.parse.length !== 3)
{
throw "Module parse(..) method should have 3 parameters (html, date, callback)";
}
var id = config.restaurants[i].id;
if (typeof actions[id] !== "undefined")
{
throw "Non unique id '" + id + "' provided";
}
var url = config.restaurants[i].url;
var postParams = config.restaurants[i].post;
actions[id] = createAction(url, postParams, parserModule.parse);
}
catch (e)
{
console.warn(e);
}
}
if (Object.keys(actions).length === 0)
{
console.error("Initialization failed, exiting");
process.exit(1);
}
console.log("Initialization successful (" + Object.keys(actions).length + " of " + config.restaurants.length + ")");
console.log("Registering partials...");
hbs.registerPartials(__dirname + '/views/partials');
console.log("Done");
console.log("Global setup...");
moment.locale('sk');
moment.tz.setDefault("Europe/Bratislava");
console.log("Done");
console.log("Express setup...");
var app = express();
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.static('static'));
app.get('/:theme?', function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.setHeader('Content-Language', 'sk');
var theme = parserUtil.parseTheme(req);
res.cookie('theme', theme, { maxAge: 315360000000, httpOnly: true });
res.render(config.themes[theme].template, { restaurants: config.restaurants, themes: config.themes });
});
app.get('/menu/:id/:day', function(req, res) {
if (typeof actions[req.params.id] === "undefined")
{
res.statusCode = 404;
res.send("Restaurant " + req.params.id + " not found\n");
}
else
{
actions[req.params.id](moment(req.params.day, "YYYY-MM-DD"), function(error, cachedMenu) {
if (error)
{
res.statusCode = 500;
res.send(error.toString());
}
else
{
res.json(cachedMenu ? { menu: cachedMenu.value, timeago: moment(cachedMenu.timestamp).fromNow() } : null);
}
});
}
});
console.log("Done");
console.log("Creating server...");
app.listen(config.port, function(err) {
if(err){
console.error("Unable to create server", err);
process.exit(1);
return;
}
var host = this.address().address;
var port = this.address().port;
console.log('Done, listening on http://%s:%s', host, port);
});