-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
237 lines (194 loc) · 6.09 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var http = require('http'),
path = require('path'),
hbs = require ('hbs'),
express = require('express'),
slugify = require('./slug'),
marked = require('marked'),
_ = require('lodash'),
fs = require('fs'),
app = express();
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
app.disable('x-powered-by');
app.use((req, res, next) => {
res.setHeader('x-powered-by', 'ffconf');
next();
});
require('datejs');
var data = {},
locations = require('./data/locations'),
sessions = require('./data/sessions'),
sponsors = require('./data/sponsors'),
workshops = require('./data/workshops'),
sideview = require('./data/sideview'),
configFile = __dirname + '/data/config.json',
config = {};
function updateConfig(blocking) {
var update = function (err, data) {
if (err) {
console.error('failed to read config', err);
return;
}
config = JSON.parse(data);
app.set('mode', config.mode);
app.set('soldout', config.soldout);
app.set('onsale', config.onsale);
app.set('videos', config.videos);
workshops.workshops.forEach(function (workshop) {
workshop.soldout = config.soldout[workshop.slug];
});
};
if (blocking === true) {
try {
update(null, fs.readFileSync(configFile, 'utf8'));
} catch (e) {
console.error('failed to read sync config', e);
}
} else {
fs.readFile(configFile, 'utf8', update);
}
}
// watch the config which sets the mode of the site and soldout state
updateConfig(true); // read synchonously on boot
sessions = (function (sessionData) {
/*
titles = titles and descriptions only
speakers = titles, descriptions and available speaker information (name, photo, etc)
schedule = full schedule with all data and times
*/
var tempSessions = [],
sessions = sessionData.sessions,
startTime = new Date(sessionData.startTime);
// TODO: This section can probably be made a lot cleaner
// with some map reduce pluck vooodoo
if (app.settings.mode === "titles") {
sessions.forEach(function (session) {
if (session.break) return;
tempSessions.push({
title: session.title,
description: session.description,
slug: session.slug,
background: session.background
});
});
};
if (app.settings.mode === "speakers") {
sessions.forEach(function (session) {
if (session.break) return;
tempSessions.push(session);
/*
tempSessions.push({
title: session.title,
description: session.description,
speaker: session.speaker,
slug: session.slug,
background: session.background
});
*/
});
}
if (app.settings.mode === "schedule") {
sessions.forEach(function (session) {
session.date = startTime.getTime();
if (!session.start) session.start = startTime.clone().toString('HH:mm');
if (!session.end) session.end = startTime.add({ minutes: session.duration }).clone().toString('HH:mm');
});
tempSessions = sessions;
}
// slugify all titles
tempSessions.forEach(function (session) {
session.slug = slugify(session.title);
});
return {
sessions: tempSessions
};
})(sessions);
sessions.sessions.forEach(function (session) {
return;
if ( (session.speaker && session.speaker.twitter)
|| session.slides
|| session.audio
|| session.video
) {
session.links = true;
}
});
_.assign(data, locations, sessions, sponsors, workshops);
data.offline = true;
app.set('isproduction', process.NODE_ENV === 'production');
// RS: run sass compile from command line to allow for devtools
// sourcemap support: sass --watch --scss --sourcemap public/sass/fullfrontal.scss:public/fullfrontal.css
// NOTE: requires sass 3.x - installed via gem install sass --pre as of July 6, 2013
app.set('port', process.env.PORT || 8000);
app.use(favicon(path.join(__dirname, 'public/favicon.ico')));
app.set('views', __dirname + '/views');
app.set('view engine' ,'hbs');
// app.use(express.logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
hbs.registerPartials(__dirname + '/views/partials');
hbs.registerHelper('markdown', function (options) {
return marked(options.fn(this));
});
hbs.registerHelper('noorphan', function (context, options) {
var i = context.lastIndexOf(' ');
return new hbs.handlebars.SafeString(context.substr(0, i) + ' ' + context.substr(i + 1));
});
// hbs.registerHelper('log', function (context) {
// console.log(context);
// });
hbs.registerHelper('link', function () {
var text = Object.keys(this)[0];
var url = this[text];
text = hbs.handlebars.Utils.escapeExpression(text);
url = hbs.handlebars.Utils.escapeExpression(url);
var result = '<a class="button" href="' + url + '">' + text + '</a>';
return new hbs.handlebars.SafeString(result);
});
app.get('/', function (req, res) {
res.render('index', data);
});
app.get('/workshop', function (req, res) {
res.render('workshop-full', workshops);
});
app.get('/sideview', function (req, res) {
res.render('sideview', sideview);
});
app.get('/workshop/:slug', function (req, res) {
res.render('workshop-full', {
workshops: _.filter(workshops.workshops, { 'slug': req.params.slug })
});
});
/* API? */
app.get('/api/all', function (req, res) {
res.json(data);
});
app.get('/api/sessions', function (req, res) {
res.json(sessions);
});
app.get('/api/locations', function (req, res) {
res.json(locations);
});
app.get('/api/sponsors', function (req, res) {
res.json(sponsors);
});
app.get('/api/workshops', function (req, res) {
res.json(workshops);
});
app.get('/details', function (req, res) {
fs.readFile('public/details.html', 'utf8', function (err, data) {
res.send(data);
});
});
app.get('/sponsorship', function (req, res) {
res.render('sponsorship');
});
app.use(express.static(path.join(__dirname, 'public')));
if (module.parent) {
module.exports = app;
} else {
http.createServer(app).listen(app.get('port'), function (){
console.log("Express server listening on http://localhost:" + app.get('port'));
});
}