-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
270 lines (224 loc) · 7.95 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import rateLimit from 'express-rate-limit';
import Rollbar from 'rollbar';
import { I18n } from 'i18n';
import { Feed } from 'feed';
import { marked } from 'marked';
import * as viewHelpers from './view-helpers.js';
import * as data from './data.js';
let rollbar;
if (process.env.ROLLBAR_ACCESS_TOKEN) {
console.log('Rollbar enabled');
rollbar = new Rollbar({
accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
captureUncaught: true,
captureUnhandledRejections: true
});
} else {
console.log('Rollbar disabled');
}
const i18n = new I18n({
locales: ['en', 'zh-tw'],
directory: 'locales',
updateFiles: false,
missingKeyFn: function (locale, value) {
const prefixes = ['Country', 'Location', 'City', 'Area', 'Station', 'Type', 'Data', 'Button', 'Title', 'Page'];
for (const prefix of prefixes) {
if (value.startsWith(prefix + ':')) {
if (prefix == 'Station') {
return value.split(':').slice(1).join(':').slice(1).split(' / ').slice(1).join(' / ');
} else {
return value.split(':').slice(1).join(':').slice(1);
}
}
}
return value;
},
});
const app = express();
const port = 3000;
app.set('views', 'views');
app.set('view engine', 'pug');
app.set('strict routing', true);
const DEBUG = process.env.NODE_ENV !== 'production';
// Disable redirect, otherwise the middleware auto creates redirects for directories
// E.g. /taipei -> /taipei/ which conflicts with the city urls!
app.use(express.static('public', { redirect: false, maxAge: DEBUG ? 0 : '1y' }));
app.use(express.static('images', { redirect: false, maxAge: DEBUG ? 0 : '1y' }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.locals.site = {
title: 'Cafe and Cowork',
summary: 'Title: Find Places to Work From',
description: 'A curated collection of work-friendly cafes and coworking spaces around the world. Find the best places with power outlets and fast WiFi to work or study from.',
url: DEBUG ? 'http://localhost:3000' : 'https://cafeandcowork.com',
github: 'https://github.com/pqvst/cafeandcowork',
instagram: 'https://instagram.com/cafeandcowork',
mailto: 'mailto:[email protected]',
};
app.locals.DEBUG = DEBUG;
app.locals.pretty = DEBUG;
app.locals.v = Date.now();
app.locals.marked = marked;
Object.assign(app.locals, viewHelpers);
Object.assign(app.locals, data.load());
function redirectWithTrailingSlash(req, res) {
res.redirect(301, req.path + '/' + req.url.slice(req.path.length));
}
app.use(i18n.init);
for (const locale of i18n.getLocales()) {
const prefix = locale == 'en' ? '' : `/${locale}`;
app.use(`${prefix}/`, (req, res, next) => {
req.setLocale(locale);
res.locals.prefix = prefix;
res.locals.site = Object.assign({}, app.locals.site, {
title: res.__(app.locals.site.title),
summary: res.__(app.locals.site.summary),
description: res.__(app.locals.site.description),
});
next();
});
if (prefix) {
app.get(`${prefix}`, redirectWithTrailingSlash);
}
function listHandler(prefix, list) {
return (req, res) => {
const pages = Math.ceil(list.length / PER_PAGE);
const page = req.params.page == null ? 1 : Number(req.params.page);
if (isNaN(page) || page <= 0 || page > pages) {
throw new Error('Invalid page');
}
const i = (page - 1) * PER_PAGE;
res.render('list', {
url: page > 1 ? `/${prefix}/${page}/` : `/${prefix}/`,
list: list.slice(i, i + PER_PAGE),
nav: { recent: true },
page,
pages,
prev: page > 1 ? `/${prefix}/${page - 1}/` : null,
next: page < pages ? `/${prefix}/${page + 1}/` : null,
});
};
}
const PER_PAGE = 10;
app.get(`${prefix}/`, listHandler('recent', app.locals.recent));
app.get(`${prefix}/recent`, redirectWithTrailingSlash);
app.get(`${prefix}/recent/`, listHandler('recent', app.locals.recent));
app.get(`${prefix}/recent/:page`, redirectWithTrailingSlash);
app.get(`${prefix}/recent/:page/`, listHandler('recent', app.locals.recent));
app.get(`${prefix}/top`, redirectWithTrailingSlash);
app.get(`${prefix}/top/`, listHandler('top', app.locals.top));
app.get(`${prefix}/top/:page`, redirectWithTrailingSlash);
app.get(`${prefix}/top/:page/`, listHandler('top', app.locals.top));
app.get(`${prefix}/about`, redirectWithTrailingSlash);
app.get(`${prefix}/about/`, (req, res) => {
res.render('about', { url: '/about/' });
});
app.get(`${prefix}/netherlands`, redirectWithTrailingSlash);
app.get(`${prefix}/netherlands/`, (req, res) => {
res.redirect(`${prefix}/`);
});
app.get(`${prefix}/taiwan`, redirectWithTrailingSlash);
app.get(`${prefix}/taiwan/`, (req, res) => {
res.redirect(`${prefix}/`);
});
for (const city of app.locals.cities) {
const cityDescription = data.getCityDescription(i18n, locale, city);
app.get(`${prefix}/${city.id}`, redirectWithTrailingSlash);
app.get(`${prefix}/${city.id}/`, (req, res) => {
res.render('city', {
title: res.__(`City: ${city.name}`),
description: cityDescription,
url: city.url,
city
});
});
app.get(`${prefix}/api/${city.id}.json`, (req, res) => {
res.json(city);
});
for (const redirect of city.redirects) {
app.get(`${prefix}/${city.id}/${encodeURI(redirect.id)}`, redirectWithTrailingSlash);
app.get(`${prefix}/${city.id}/${encodeURI(redirect.id)}/`, (req, res) => {
res.redirect(`${prefix}/${redirect.redirect}/`);
});
}
for (const place of city.places) {
const placeDescription = data.getPlaceDescription(i18n, locale, place);
if (place.redirect_old_city) {
app.get(`${prefix}/${place.redirect_old_city}/${encodeURI(place.id)}`, redirectWithTrailingSlash);
app.get(`${prefix}/${place.redirect_old_city}/${encodeURI(place.id)}/`, (req, res) => {
res.redirect(`${prefix}/${city.id}/${encodeURI(place.id)}/`);
});
}
app.get(`${prefix}/${city.id}/${encodeURI(place.id)}`, redirectWithTrailingSlash);
app.get(`${prefix}/${city.id}/${encodeURI(place.id)}/`, (req, res) => {
res.render('place', {
title: place.name,
description: placeDescription,
url: place.url,
image: place.images && place.images.length > 0 ? place.images[0] : null,
city,
place
});
});
}
}
}
const submissionLimiter = rateLimit({
keyGenerator: () => 'all',
windowMs: 5 * 60 * 1000, // 5 minutes
max: 10, // limit to 10 submissions per 5 minutes
});
app.get('/suggest', redirectWithTrailingSlash);
app.get('/suggest/', (req, res) => {
res.redirect('/');
});
app.get('/submit', redirectWithTrailingSlash);
app.get('/submit/', (req, res) => {
res.redirect('/');
});
app.get('/feed.xml', (req, res) => {
res.type('application/xml');
const locale = req.locale;
const site = app.locals.site;
const feed = new Feed({
title: site.title,
description: site.description,
id: site.url,
link: site.url,
language: locale,
image: `${site.url}/share.png`,
updated: new Date,
});
app.locals.recent.forEach(item => {
const placeDescription = data.getPlaceDescription(i18n, locale, item);
feed.addItem({
title: item.name,
id: `${site.url}${item.url}`,
link: `${site.url}${item.url}`,
description: placeDescription,
content: placeDescription,
date: new Date(item.updated || item.added),
image: item.images ? `${site.url}${item.images[0]}` : null,
});
});
res.send(feed.rss2());
//res.render('feed', { recent: app.locals.recent, pretty: true });
});
if (DEBUG) {
app.get('/rollbar', (req, res) => {
throw new Error('Keep Rollbar Active');
});
}
app.use((req, res) => {
res.status(404).render('error');
});
if (rollbar) {
app.use(rollbar.errorHandler());
}
app.listen(port);
process.on('SIGTERM', () => {
process.exit();
});