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;
|
||||
|
|
|
@ -224,10 +224,10 @@ export function* list() {
|
|||
}
|
||||
|
||||
|
||||
export function* get(id) {
|
||||
export function* get() {
|
||||
const Files = this.db.Files;
|
||||
const Users = this.db.Users;
|
||||
const file = yield Files.findOne({_id: id, status: {'$in': ['active', 'uploading']}});
|
||||
const file = yield Files.findOne({_id: this.params.id, status: {'$in': ['active', 'uploading']}});
|
||||
this.assert(file, 404, '{"error": {"message": "File not found", "code": 604}}');
|
||||
const user = yield Users.findOne({_id: file.owner});
|
||||
this.assert(user && !user.banned, 404, '{"error": {"message": "File not found", "code": 604}}');
|
||||
|
@ -236,22 +236,23 @@ export function* get(id) {
|
|||
}
|
||||
|
||||
|
||||
export function* put(id) {
|
||||
export function* put() {
|
||||
if (this.request.body.trashed) {
|
||||
const Files = this.db.Files;
|
||||
const status = this.request.body.trashed ? 'trashed' : 'active';
|
||||
yield Files.updateOne({'_id': id, owner: this.user.id}, {$set: {status: status}}, {w: 1});
|
||||
yield Files.updateOne({'_id': this.params.id, owner: this.user.id}, {$set: {status: status}}, {w: 1});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function* del(id) {
|
||||
export function* del() {
|
||||
const Files = this.db.Files;
|
||||
yield Files.updateOne({'_id': id, owner: this.user.id}, {$set: {status: 'deleted'}}, {w: 1});
|
||||
const event = {type: 'file-deleted', data: {'id': id}};
|
||||
yield Files.updateOne({'_id': this.params.id, owner: this.db.ObjectId(this.user.id)}, {$set: {status: 'deleted'}}, {w: 1});
|
||||
const event = {type: 'file-deleted', data: {'id': this.params.id}};
|
||||
yield this.redis.publish('/user/' + this.user.id, JSON.stringify(event));
|
||||
yield this.redis.publish('/file/' + id, JSON.stringify(event));
|
||||
yield this.redis.publish('/file/' + this.params.id, JSON.stringify(event));
|
||||
this.statsd.incr('file.delete', 1);
|
||||
this.status = 204;
|
||||
this.body = '';
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue