2017-05-07 00:02:20 +01:00
|
|
|
import fs from 'fs';
|
2015-06-03 21:45:54 -07:00
|
|
|
import path from 'path';
|
2015-08-20 23:22:57 +01:00
|
|
|
import zlib from 'zlib';
|
2015-06-03 21:45:54 -07:00
|
|
|
import koa from 'koa';
|
2015-11-29 14:10:53 +00:00
|
|
|
import cors from 'kcors';
|
2015-06-03 21:45:54 -07:00
|
|
|
import route from 'koa-route';
|
|
|
|
import logger from 'koa-logger';
|
|
|
|
import favicon from 'koa-favicon';
|
|
|
|
import compress from 'koa-compress';
|
2016-10-03 13:31:29 +01:00
|
|
|
import serve from 'koa-static';
|
|
|
|
import views from 'koa-views';
|
2015-06-03 21:45:54 -07:00
|
|
|
import bodyparser from 'koa-bodyparser';
|
2020-01-18 10:19:20 +00:00
|
|
|
import Sentry from '@sentry/node';
|
2016-10-03 13:31:29 +01:00
|
|
|
import debuglog from 'debug';
|
2020-01-18 10:19:20 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
|
|
|
import index from './routes/index.js';
|
|
|
|
import recent from './routes/recent.js';
|
|
|
|
import search from './routes/search.js';
|
|
|
|
import share from './routes/share.js';
|
|
|
|
import { slack, oauth } from './routes/slack.js';
|
|
|
|
import errorHandler from './lib/error-handler.js';
|
2020-08-21 11:15:06 +01:00
|
|
|
import newrelic from 'newrelic';
|
2021-12-11 14:58:56 +00:00
|
|
|
import { constants } from 'zlib'
|
2015-06-03 21:45:54 -07:00
|
|
|
|
2017-10-23 17:58:23 +01:00
|
|
|
const debug = debuglog('combine.fm');
|
2015-06-03 21:45:54 -07:00
|
|
|
|
2020-01-18 10:19:20 +00:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
process.env.VUE_ENV = 'server';
|
2015-06-03 21:45:54 -07:00
|
|
|
|
2019-03-11 21:03:44 +00:00
|
|
|
Sentry.init({
|
|
|
|
dsn: process.env.SENTRY_DSN,
|
|
|
|
});
|
2017-05-26 18:33:18 +01:00
|
|
|
|
2018-04-13 21:44:31 +01:00
|
|
|
const app = new koa();
|
2014-12-14 00:23:15 +00:00
|
|
|
|
2019-03-12 00:24:06 +00:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
app.proxy = true;
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:54:02 +01:00
|
|
|
app.on('error', (err, ctx) => {
|
2019-03-12 00:29:47 +00:00
|
|
|
if (!err.status || err.status >= 500) {
|
2020-04-28 19:54:02 +01:00
|
|
|
Sentry.withScope(function(scope) {
|
|
|
|
scope.addEventProcessor(function(event) {
|
|
|
|
return Sentry.Handlers.parseRequest(event, ctx.request);
|
|
|
|
});
|
|
|
|
Sentry.captureException(err);
|
2020-06-14 22:49:49 +01:00
|
|
|
debug(err);
|
2020-04-28 19:54:02 +01:00
|
|
|
});
|
2019-03-12 00:29:47 +00:00
|
|
|
}
|
2017-05-26 18:33:18 +01:00
|
|
|
});
|
|
|
|
|
2019-03-11 21:03:44 +00:00
|
|
|
app.use(errorHandler(Sentry));
|
2016-10-23 23:04:32 +01:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(bodyparser());
|
2015-11-29 14:10:53 +00:00
|
|
|
app.use(cors());
|
2021-12-11 14:58:56 +00:00
|
|
|
app.use(compress({
|
|
|
|
filter (content_type) {
|
|
|
|
return /text/i.test(content_type)
|
|
|
|
},
|
|
|
|
threshold: 2048,
|
|
|
|
gzip: {
|
|
|
|
flush: constants.Z_SYNC_FLUSH
|
|
|
|
},
|
|
|
|
deflate: {
|
|
|
|
flush: constants.Z_SYNC_FLUSH,
|
|
|
|
},
|
|
|
|
br: false
|
|
|
|
}));
|
2020-06-14 22:49:49 +01:00
|
|
|
app.use(favicon(path.join(__dirname, '/public/assets/images/favicon.png')));
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(logger());
|
2016-10-03 13:31:29 +01:00
|
|
|
app.use(serve('public', { maxage: 31536000000 }));
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2017-05-07 00:02:20 +01:00
|
|
|
const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, '/public/dist/manifest.json')));
|
|
|
|
|
2018-04-13 21:44:31 +01:00
|
|
|
app.use(async function(ctx, next) {
|
2019-03-12 00:24:06 +00:00
|
|
|
Sentry.configureScope(scope => {
|
|
|
|
scope.setExtra('Request', {
|
|
|
|
'url': ctx.request.url,
|
|
|
|
'method': ctx.request.method,
|
|
|
|
'user-agent': ctx.request.header['user-agent'],
|
|
|
|
});
|
|
|
|
});
|
2018-04-13 21:44:31 +01:00
|
|
|
ctx.state.manifest = manifest;
|
|
|
|
await next();
|
2017-05-07 00:02:20 +01:00
|
|
|
});
|
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
app.use(views(path.resolve(__dirname, './views'), {
|
|
|
|
map: {
|
|
|
|
html: 'ejs',
|
|
|
|
},
|
|
|
|
}));
|
2014-12-03 23:32:33 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(route.get('/', index));
|
2016-10-03 13:31:29 +01:00
|
|
|
app.use(route.get('/recent', recent));
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(route.post('/search', search));
|
|
|
|
app.use(route.get('/:service/:type/:id.:format?', share));
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2018-04-19 22:03:49 +01:00
|
|
|
app.use(route.post('/slack', slack));
|
2018-04-19 22:35:51 +01:00
|
|
|
app.use(route.get('/slack', slack));
|
2018-04-28 17:55:02 +01:00
|
|
|
app.use(route.get('/oauth', oauth));
|
2018-04-19 22:03:49 +01:00
|
|
|
|
2020-01-18 10:19:20 +00:00
|
|
|
if (process.argv[1] === __filename) {
|
2016-10-03 13:31:29 +01:00
|
|
|
app.listen(process.env.PORT || 3000, () => {
|
|
|
|
debug(`Koa HTTP server listening on port ${(process.env.PORT || 3000)}`);
|
2015-08-20 23:22:57 +01:00
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
|
|
|
|
export default app;
|