/**
* Created by zdy on 2016/9/28.
* 应用的核心类
*/
'use strict';
const Path = require('path');
const Koa = require('koa');
const convert = require('koa-convert');
const serve = require('koa-static');
const favicon = require('koa-favicon');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');
const render = require('koa-swig');
class App extends Koa {
constructor(config) {
super();
// 配置
this.config = config;
// 日志
this.logger = require('./logger')('direwolf');
// 静态文件
this.use(convert(serve(Path.join(this.config.baseDir, 'public'))));
// favicon
this.use(convert(favicon(Path.join(this.config.baseDir, 'public/favicon.ico'))));
// session
this.use(convert(session(this)));
// bodyParser
this.use(convert(bodyParser()));
// controller
this.controller = require('./controller')(this);
// router
this.router = require('./router')(this);
// views
this.context.render = render({
root: Path.join(this.config.baseDir, 'views'),
autoescape: true,
cache: false,
ext: 'html',
locals: {},
filters: {},
tags: {},
extensions: {}
});
// service
this.ServiceCore = require('./service');
this.context.service = new this.ServiceCore(this);
}
}
module.exports = App; |