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';
|
2015-06-15 02:34:39 +01:00
|
|
|
import staticHandler from 'koa-file-server';
|
2015-06-03 21:45:54 -07:00
|
|
|
import bodyparser from 'koa-bodyparser';
|
|
|
|
import co from 'co';
|
|
|
|
import db from './config/db';
|
|
|
|
import index from './routes/index';
|
|
|
|
import search from './routes/search';
|
|
|
|
import share from './routes/share';
|
|
|
|
import itunesProxy from './routes/itunes-proxy';
|
2015-10-26 17:20:32 +00:00
|
|
|
|
|
|
|
import React from 'react';
|
2015-08-20 23:22:57 +01:00
|
|
|
import { routes } from './views/app';
|
2015-08-21 18:33:50 +01:00
|
|
|
import errorHandler from './lib/error-handler';
|
2015-06-03 21:45:54 -07:00
|
|
|
|
|
|
|
import debuglog from 'debug';
|
|
|
|
const debug = debuglog('match.audio');
|
|
|
|
|
|
|
|
const app = koa();
|
|
|
|
|
2015-08-21 18:33:50 +01:00
|
|
|
app.use(errorHandler(routes));
|
2014-12-14 00:23:15 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(bodyparser());
|
2015-11-29 14:10:53 +00:00
|
|
|
app.use(cors());
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(compress({flush: zlib.Z_SYNC_FLUSH }));
|
|
|
|
app.use(favicon(path.join(__dirname, '/public/images/favicon.png')));
|
|
|
|
app.use(logger());
|
2015-06-15 02:34:39 +01:00
|
|
|
app.use(staticHandler({root: 'public', maxage: 31536000000}));
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
let mongo = {};
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
co(function*() {
|
|
|
|
mongo = yield db();
|
2014-12-13 12:05:47 +00:00
|
|
|
});
|
2014-12-05 16:26:01 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(function* (next){
|
|
|
|
this.db = mongo;
|
|
|
|
yield next;
|
|
|
|
});
|
2014-12-07 18:33:18 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(function* (next) {
|
2014-12-07 18:33:18 +00:00
|
|
|
// force SSL
|
2015-06-03 21:45:54 -07:00
|
|
|
if (this.headers['cf-visitor'] && this.headers['cf-visitor'] !== '{"scheme":"https"}') {
|
|
|
|
return this.redirect('https://' + this.headers.host + this.url);
|
|
|
|
} else if (this.headers['cf-visitor']) {
|
|
|
|
this.userProtocol = 'https';
|
2014-12-03 23:32:33 +00:00
|
|
|
} else {
|
2015-06-03 21:45:54 -07:00
|
|
|
this.userProtocol = 'http';
|
2014-12-03 23:32:33 +00:00
|
|
|
}
|
2014-12-07 18:33:18 +00:00
|
|
|
// redirect www
|
2015-06-03 21:45:54 -07:00
|
|
|
if (this.headers.host.match(/^www/) !== null ) {
|
|
|
|
return this.redirect(this.userProtocol + '://' + this.headers.host.replace(/^www\./, '') + this.url);
|
2014-12-07 18:33:18 +00:00
|
|
|
} else {
|
2015-06-03 21:45:54 -07:00
|
|
|
yield next;
|
2014-12-07 18:33:18 +00:00
|
|
|
}
|
2014-12-03 23:32:33 +00:00
|
|
|
});
|
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
app.use(route.get('/', index));
|
|
|
|
app.use(route.post('/search', search));
|
|
|
|
app.use(route.get('/itunes/(.*)', itunesProxy));
|
|
|
|
app.use(route.get('/:service/:type/:id.:format?', share));
|
|
|
|
app.use(route.get('/recent', function* () {
|
|
|
|
let recents = [];
|
|
|
|
let docs = yield this.db.matches.find().sort({'created_at': -1}).limit(6).toArray();
|
|
|
|
docs.forEach(function(doc) {
|
|
|
|
recents.push(doc.services[doc._id.split('$$')[0]]); // eslint-disable-line no-underscore-dangle
|
2014-12-22 22:38:08 +00:00
|
|
|
});
|
2015-06-03 21:45:54 -07:00
|
|
|
this.body = {recents: recents};
|
|
|
|
}));
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
if (!module.parent) {
|
2015-08-20 23:22:57 +01:00
|
|
|
app.listen(process.env.PORT || 3000, function() {
|
|
|
|
debug('Koa HTTP server listening on port ' + (process.env.PORT || 3000));
|
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|
2015-08-20 23:22:57 +01:00
|
|
|
|
|
|
|
export default app;
|