combine.fm/app.js

110 lines
3 KiB
JavaScript
Raw Normal View History

2015-06-03 21:45:54 -07:00
import path from 'path';
import koa from 'koa';
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 React from 'react';
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-06-15 02:34:39 +01:00
import {routes} from './views/app';
2015-06-03 21:45:54 -07:00
import zlib from 'zlib';
import createHandler from './lib/react-handler';
import debuglog from 'debug';
const debug = debuglog('match.audio');
const app = koa();
app.use(function* (next) {
this.set('Server', 'Nintendo 64');
try {
yield next;
} catch (err) {
if (!err.status) {
2015-07-23 08:51:00 +01:00
debug('Error: %o', err);
2015-06-03 21:45:54 -07:00
} else if (err.status === 404) {
let Handler = yield createHandler(routes, this.request.url);
let App = React.createFactory(Handler);
let content = React.renderToString(new App());
this.body = '<!doctype html>\n' + content;
} else {
2015-07-23 08:51:00 +01:00
debug('Error: %o', err);
2015-06-03 21:45:54 -07:00
throw err;
}
}
});
2015-06-03 21:45:54 -07:00
app.use(bodyparser());
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
});
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
});
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
module.exports = app;
2015-05-17 19:10:30 +01:00
2015-06-03 21:45:54 -07:00
if (!module.parent) {
2015-06-15 02:34:39 +01:00
if (process.env.LOCALHOST_KEY) {
require('spdy').createServer({
key: process.env.LOCALHOST_KEY,
cert: process.env.LOCALHOST_CRT
}, app.callback()).listen(3000, function() {
debug('Koa SPDY server listening on port ' + (process.env.PORT || 3000));
});
} else {
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
}