const Koa = require("koa") const load = require("./load") const path = require('path') const xe = require("xe-utils") const EventEmitter = require('events'); const Router = require("koa-router"); const Parameter = require('parameter'); const error = require('./errorException'); const log4js = require("log4js"); const Smf = require('./smf/smf'); module.exports = class Bamboo extends Koa { constructor() { super(); this.init() } path(args) { return this.isPath + '/' + (args || "") } isPath(args) { return path.resolve('./' + (args || "")) } get xe() { return xe } async init() { this.config = {} await this.loadConfig() this.event = new EventEmitter() this.parameter = new Parameter(); this.error = error; this.smf = new Smf() log4js.configure(this.config['log']); this.logger = log4js.getLogger(); // await this.loadEvent() // await this.loadStatus() // await this.loadMiddleware() // await this.loadController() } /** * 加载配置 */ async loadConfig() { this.config = {} const config = await load(this.isPath(), "config") config.forEach(item => { this.config[item.file_name] = item.res }) } // // /** // * 加载事件 // */ // async loadEvent() { // const event = await load(this.isPath(), "event") // event.forEach(item => { // Object.keys(item.res).forEach(key => { // this.event.on(key, item.res[key]) // }) // }) // } // // /** // * 加载状态 // */ // async loadStatus() { // const status = await load(this.isPath(), "status") // const setSmfDataList = [] // status.forEach(item => { // setSmfDataList.push(item.res) // }) // this.smf.setSmfDataList(setSmfDataList) // //监听流程变更 // this.smf.on((e, paras) => { // this.event.emit(e.currentState, paras) // }) // } // // /** // * 加载控制器路由 // */ // async loadController() { // const controller = await load(this.isPath(), "controller") // const router = new Router(); // controller.forEach(item => { // item.res.method.forEach(method => { // router[method](item.res.path, async (ctx, next) => { // const validate = this.parameter.validate(item.res.params, ctx.request.body); // if (validate) { // console.error(validate); // this.error.ParameterException(validate); // } // else { // await item.res.fun(ctx, this.application) // await next(); // } // }) // }) // }) // } // // /** // * 加载中间件 // */ // async loadMiddleware() { // const middleware = await load(this.isPath(), "middleware") // let hashList = xe.toArray(middleware) // hashList = xe.orderBy(hashList, "fun.sort") // hashList = hashList.filter(item => item.res.use) // hashList.forEach(item => { // this.use(async (ctx, next) => { // ctx['logger'] = this.logger // return await item.res.fun(ctx, next, this.application) // }) // }) // } }