All files / lib Lark.js

97.62% Statements 41/42
100% Branches 2/2
90% Functions 9/10
97.56% Lines 40/41
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77          1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x   1x 1x         8x   3x 10x 1x     8x 8x         8x 8x 8x 8x 8x 8x   8x       9x 1x   8x   8x 8x       9x 9x   9x   8x   8x 8x     8x 8x       1x  
/**
 * Class Lark
 **/
'use strict';
 
const assert          = require('assert');
const debug           = require('debug')('lark.Lark');
const path            = require('path');
 
const Koa             = require('koa');
const LarkConfig      = require('lark-config');
const LarkMVC         = require('lark-mvc');
const LarkRouter      = require('lark-router');
const LarkRoutes      = require('lark-router-config');
 
const loadPackage     = require('./load-package');
const loadConfigs     = require('./load-configs');
const loadMiddlewares = require('./load-middlewares');
 
const DEFAULT_CONFIG_DIRECTORY  = path.join(__dirname, '../configs');
const CONFIG_NAME_SERVER_PORT   = 'server/port';
 
class Lark extends Koa {
 
    static get DEFAULT_CONFIG_DIRECTORY() { return DEFAULT_CONFIG_DIRECTORY; }
    static get CONFIG_NAME_SERVER_PORT() { return CONFIG_NAME_SERVER_PORT; }
 
    static get Model() { return LarkMVC.Model; }
    static get Controller() { return LarkMVC.Controller; }
    static get View() { return LarkMVC.View; }
 
    constructor(...args) {
        debug('constructing');
        super(...args);
        
        /**
         * Initialize properties
         **/
        debug('initialize properties');
        this.package  = loadPackage();
        this.config   = new LarkConfig();
        this.router   = new LarkRouter();
        this.routes   = new LarkRoutes();
        this.mvc      = new LarkMVC();
 
        debug('constructing done');
    }
 
    async initialize() {
        if (this._initialized) {
            return this;
        }
        this._initialized = true;
 
        await loadConfigs(this);
        await loadMiddlewares(this);
    }
 
    async start() {
        debug('starting an app');
        await this.initialize();
 
        assert(this.config.has(CONFIG_NAME_SERVER_PORT),
            `No server port found in config["${CONFIG_NAME_SERVER_PORT}"]`);
        const port = this.config.get(Lark.CONFIG_NAME_SERVER_PORT);
 
        const service = await new Promise(resolve => {
            let server = this.listen(port, () => resolve({port, server}));
        });
 
        debug('app started!');
        return service;
    }
}
 
module.exports = Lark;