Migrate from koa-route to koa-router
This commit is contained in:
parent
8474cca94c
commit
a4bf3dd51d
17 changed files with 206 additions and 291 deletions
93
api/app.js
93
api/app.js
|
@ -1,63 +1,29 @@
|
|||
import koa from 'koa';
|
||||
import route from 'koa-route';
|
||||
import Router from 'koa-router';
|
||||
import stats from 'koa-statsd';
|
||||
import websockify from 'koa-websocket';
|
||||
import logger from 'koa-logger';
|
||||
import compress from 'koa-compress';
|
||||
import bodyparser from 'koa-bodyparser';
|
||||
import cors from 'kcors';
|
||||
import co from 'co';
|
||||
import raven from 'raven';
|
||||
import StatsD from 'statsy';
|
||||
import auth from './lib/auth';
|
||||
import mongo from '../lib/mongo';
|
||||
import redis from '../lib/redis';
|
||||
import * as user from './routes/user';
|
||||
import * as file from './routes/file';
|
||||
import debugname from 'debug';
|
||||
const debug = debugname('hostr-api');
|
||||
import StatsD from 'statsy';
|
||||
|
||||
if (process.env.SENTRY_DSN) {
|
||||
const ravenClient = new raven.Client(process.env.SENTRY_DSN);
|
||||
ravenClient.patchGlobal();
|
||||
}
|
||||
|
||||
const app = websockify(koa());
|
||||
|
||||
app.use(function* (next){
|
||||
this.set('Server', 'Nintendo 64');
|
||||
if(this.req.headers['x-forwarded-proto'] === 'http'){
|
||||
return this.redirect('https://' + this.req.headers.host + this.req.url);
|
||||
}
|
||||
yield next;
|
||||
});
|
||||
const router = new Router();
|
||||
|
||||
let statsdOpts = {prefix: 'hostr-api', host: process.env.STATSD_HOST || 'localhost'};
|
||||
let statsd = new StatsD(statsdOpts);
|
||||
app.use(function*(next) {
|
||||
router.use(function*(next) {
|
||||
this.statsd = statsd;
|
||||
yield next;
|
||||
});
|
||||
app.use(stats(statsdOpts));
|
||||
router.use(stats(statsdOpts));
|
||||
|
||||
app.use(logger());
|
||||
|
||||
app.use(cors({
|
||||
router.use(cors({
|
||||
origin: '*',
|
||||
credentials: true
|
||||
}));
|
||||
|
||||
app.use(mongo());
|
||||
app.use(redis());
|
||||
app.ws.use(mongo());
|
||||
app.ws.use(redis());
|
||||
|
||||
app.use(route.get('/', function* (){
|
||||
this.status = 200;
|
||||
this.body = '';
|
||||
}));
|
||||
|
||||
app.use(function* (next){
|
||||
router.use('/*',function* (next) {
|
||||
try {
|
||||
yield next;
|
||||
if (this.response.status === 404 && !this.response.body) {
|
||||
|
@ -90,34 +56,21 @@ app.use(function* (next){
|
|||
this.type = 'application/json';
|
||||
});
|
||||
|
||||
app.use(compress());
|
||||
app.use(bodyparser());
|
||||
router.get('/user', auth, user.get);
|
||||
router.get('/user/token', auth, user.token);
|
||||
router.get('/token', auth, user.token);
|
||||
router.get('/user/transaction', auth, user.transaction);
|
||||
router.post('/user/settings', auth, user.settings);
|
||||
router.get('/file', auth, file.list);
|
||||
router.post('/file', auth, file.post);
|
||||
router.get('/file/:id', file.get);
|
||||
router.put('/file/:id', auth, file.put);
|
||||
router.delete('/file/:id', auth, file.del);
|
||||
router.delete('/file/:id', auth, file.del);
|
||||
|
||||
app.ws.use(route.all('/file/:id', file.events));
|
||||
app.ws.use(route.all('/user', user.events));
|
||||
// Hack, if no route matches here, router does not dispatch at all
|
||||
router.get('/(.*)', function* () {
|
||||
this.throw(404);
|
||||
});
|
||||
|
||||
app.use(route.get('/file/:id', file.get));
|
||||
|
||||
// Run auth middleware before all other endpoints
|
||||
app.use(auth);
|
||||
|
||||
app.use(route.get('/user', user.get));
|
||||
app.use(route.get('/user/token', user.token));
|
||||
app.use(route.get('/token', user.token));
|
||||
app.use(route.get('/user/transaction', user.transaction));
|
||||
app.use(route.post('/user/settings', user.settings));
|
||||
app.use(route.get('/file', file.list));
|
||||
app.use(route.post('/file', file.post));
|
||||
app.use(route.put('/file/:id', file.put));
|
||||
app.use(route.delete('/file/:id', file.del));
|
||||
|
||||
if (!module.parent) {
|
||||
app.listen(process.env.PORT || 4042, function() {
|
||||
debug('Koa HTTP server listening on port ' + (process.env.PORT || 4042));
|
||||
});
|
||||
setInterval(function() {
|
||||
debug('%sMB', process.memoryUsage().rss / 1024 / 1024);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
export default app;
|
||||
export default router;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue