all files / direwolf/lib/core/ application.js

100% Statements 21/21
100% Branches 0/0
100% Functions 1/1
100% Lines 21/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                                                   
/**
 * 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;