Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active February 18, 2021 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/83b394e3182e4f332bded27fcf4074ca to your computer and use it in GitHub Desktop.
Save jberger/83b394e3182e4f332bded27fcf4074ca to your computer and use it in GitHub Desktop.
const Koa = require('koa');
const Router = require('@koa/router');
class Controller {
constructor(app, ctx) {
this.app = app;
this.ctx = ctx;
}
get req () { return this.ctx.request }
get res () { return this.ctx.response }
}
class App {
constructor (config = {}) {
this.config = config;
this.koa = new Koa();
this.router = new Router();
this.startup();
this.koa
.use(this.router.routes())
.use(this.router.allowedMethods());
}
to (action, cls=Controller) {
return (ctx, next) => {
const c = new cls(this, ctx);
if (action in c) {
action = c[action];
}
return action.call(c,next);
}
}
startup() {}
}
module.exports = {
App,
Controller,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment