2015-09-04 01:26:56 -04:00
|
|
|
var compression = require('compression');
|
|
|
|
var express = require('express');
|
2015-09-30 11:04:51 -04:00
|
|
|
var proxy = require('express-http-proxy');
|
2015-09-14 13:36:00 -04:00
|
|
|
var _path = require('path');
|
2015-09-04 01:26:56 -04:00
|
|
|
|
|
|
|
var handler = require('./handler');
|
|
|
|
var log = require('./log');
|
|
|
|
var routes = require('./routes.json');
|
|
|
|
|
|
|
|
// Server setup
|
|
|
|
var app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.use(log());
|
|
|
|
app.use(compression());
|
|
|
|
|
|
|
|
// Bind routes
|
2015-10-01 09:50:00 -04:00
|
|
|
for (var routeId in routes) {
|
|
|
|
var route = routes[routeId];
|
2015-09-14 12:16:58 -04:00
|
|
|
if ( route.static ) {
|
2015-09-14 13:17:50 -04:00
|
|
|
app.use( express.static( eval( route.resolve ), route.attributes ) );
|
2015-09-14 12:16:58 -04:00
|
|
|
} else {
|
2015-09-14 13:17:50 -04:00
|
|
|
app.get(route.pattern, handler(route));
|
2015-09-14 12:16:58 -04:00
|
|
|
}
|
2015-09-04 01:26:56 -04:00
|
|
|
}
|
|
|
|
|
2015-09-30 11:04:51 -04:00
|
|
|
// Bind proxies in development
|
2015-10-01 09:50:43 -04:00
|
|
|
if ( process.env.NODE_ENV != 'production' ) {
|
2015-09-30 11:04:51 -04:00
|
|
|
var proxies = require('./proxies.json');
|
|
|
|
var url = require('url');
|
2015-10-07 11:48:18 -04:00
|
|
|
var proxyHost = process.env.PROXY_HOST || 'https://staging.scratch.mit.edu';
|
2015-10-01 09:50:00 -04:00
|
|
|
for (var proxyId in proxies) {
|
|
|
|
var proxyRoute = proxies[proxyId];
|
|
|
|
app.use(proxyRoute.root, proxy(proxyRoute.proxy || proxyHost, {
|
2015-09-30 11:04:51 -04:00
|
|
|
filter: function (req) {
|
2015-10-06 13:32:51 -04:00
|
|
|
for (var pathId in proxyRoute.paths) {
|
|
|
|
var path = proxyRoute.paths[pathId];
|
|
|
|
if (url.parse(req.url).path.indexOf(path) == 0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-09-30 11:04:51 -04:00
|
|
|
},
|
|
|
|
forwardPath: function (req) {
|
|
|
|
return url.parse(req.url).path;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:26:56 -04:00
|
|
|
// Start listening
|
2015-09-16 10:40:03 -04:00
|
|
|
var port = process.env.PORT || 8333;
|
2015-09-04 01:26:56 -04:00
|
|
|
app.listen(port, function () {
|
|
|
|
process.stdout.write('Server listening on port ' + port + '\n');
|
2015-10-01 09:50:00 -04:00
|
|
|
if (proxyHost) {
|
|
|
|
process.stdout.write('Proxy host: ' + proxyHost + '\n');
|
|
|
|
}
|
2015-09-04 01:26:56 -04:00
|
|
|
});
|