Last active
February 18, 2021 16:10
-
-
Save jberger/83b394e3182e4f332bded27fcf4074ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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