forked from rgrove/rawgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
executable file
·230 lines (196 loc) · 6.1 KB
/
web.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
#!/usr/bin/env node
"use strict";
// For details on how to set up New Relic reporting, see
// https://docs.newrelic.com/docs/nodejs/configuring-nodejs-with-environment-variables
if (process.env.RAWGIT_ENABLE_NEW_RELIC) {
require('newrelic');
}
var config = require('./conf');
var express = require('express');
var hbs = require('express-handlebars');
var middleware = require('./lib/middleware');
var path = require('path');
var stats = require('./lib/stats');
var https = require('https');
var http = require('http');
// -- Configure Express --------------------------------------------------------
var app = express();
app.disable('x-powered-by');
if (app.get('env') === 'development') {
app.use(require('morgan')('dev'));
}
app.engine('handlebars', hbs({
defaultLayout: 'main',
helpers : require('./lib/helpers'),
layoutsDir : path.join(__dirname, 'views', 'layouts'),
partialsDir : path.join(__dirname, 'views', 'partials')
}));
app.set('view engine', 'handlebars');
// Need to set the views directory explicitly or RawGit will break if it's run
// from any directory other than its own root.
app.set('views', path.join(__dirname, 'views'));
app.locals.config = config;
if (process.env.GOOGLE_ANALYTICS_ID) {
app.locals.googleAnalyticsId = process.env.GOOGLE_ANALYTICS_ID;
}
app.use(express.static(config.publicDir));
// -- Routes -------------------------------------------------------------------
app.get('/', function (req, res) {
res.render('index', {includeMetaDescription: true});
});
app.get('/faq', function (req, res) {
res.render('faq', {title: 'FAQ'});
});
app.get('/stats', function (req, res) {
res.render('stats', {title: 'Usage Statistics'});
});
app.get('/stats.html', function (req, res) {
res.redirect(301, '/stats');
});
// Don't allow requests for Google Webmaster Central verification files.
app.get('*/google[0-9a-f]{16}.html',
middleware.error403);
// Public or private gist.
app.route(/^\/[0-9A-Za-z-]+\/[0-9a-f]+\/raw\/?/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://gist.githubusercontent.com'),
middleware.proxyPath('https://gist.githubusercontent.com')
);
// Bitbucket file.
app.route(/\/(.*?)\/raw\/([a-f0-9]+?)\/(.*)/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://bitbucket.org'),
middleware.proxyPath('https://bitbucket.org')
);
// gitlab.com file
app.route(/\/.+?\/.+?\/raw\/.+\/.+/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://gitlab.com'),
middleware.proxyPath('https://gitlab.com')
);
// FilesAnywhere file.
app.route(/\/fatemp\/[0-9]+?\/[0-9]+?\/[0-9A-Z]+?\/(.*)/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://personal.filesanywhere.com'),
middleware.proxyPath('https://personal.filesanywhere.com')
);
// FilesAnywhere file.
app.route(/\/in\/(.*)/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://www.linkedin.com'),
middleware.proxyPath('https://www.linkedin.com')
);
// Launchpad file.
app.route(/\/~(.+?):\/(.+)/)
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://bazaar.launchpad.net'),
middleware.proxyPath('https://bazaar.launchpad.net')
);
// Repo file.
app.route('/:user/:repo/:branch/*')
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://raw.githubusercontent.com'),
middleware.proxyPath('https://raw.githubusercontent.com')
);
// Dropbox file.
app.route('/s/:randomstring/*')
.all(
middleware.cdn,
middleware.stats,
middleware.security,
middleware.noRobots,
middleware.autoThrottle,
middleware.accessControl
)
.get(
middleware.fileRedirect('https://dl.dropboxusercontent.com'),
middleware.proxyPath('https://dl.dropboxusercontent.com')
);
// Stats API.
app.get('/api/stats', function (req, res) {
var count = Math.max(0, Math.min(20, req.query.count || 10));
res.setHeader('Cache-Control', 'private, no-cache, no-store, max-age=0, must-revalidate');
res.jsonp({
status: 'success',
data: {
since : stats.since,
files : stats.files().slice(0, count),
referrers: stats.referrers().slice(0, count)
}
});
});
// -- Error handlers -----------------------------------------------------------
app.use(function (req, res) {
res.status(404);
res.sendFile(config.publicDir + '/errors/404.html');
});
app.use(function (err, req, res, next) {
/* eslint no-unused-vars: 0 */
console.error(err.stack);
res.status(err.status || 500);
res.sendFile(config.publicDir + '/errors/500.html');
});
// -- Server -------------------------------------------------------------------
if (app.get('env') !== 'test') {
var port = process.env.PORT || 5000;
app.listen(port, function () {
console.log('Listening on port ' + port);
});
}
module.exports = app;