thinkjs/lib/think.js
2017-07-13 13:45:13 +08:00

144 lines
2.9 KiB
JavaScript

const Koa = require('koa');
const helper = require('think-helper');
const pkg = require('../package.json');
const bluebird = require('bluebird');
const assert = require('assert');
const messenger = require('think-cluster').messenger;
/**
* use bluebird instead of default Promise
*/
global.Promise = bluebird;
/**
* global think object
* @type {Object}
*/
global.think = Object.create(helper);
/**
* Koa application instance
* @type {Koa}
*/
think.app = new Koa();
/**
* think env
*/
think.__defineGetter__('env', () => think.app.env);
/**
* is cli mode
*/
think.isCli = false;
/**
* add think to think.app
*/
think.app.think = think;
/**
* thinkjs version
*/
think.version = pkg.version;
/**
* messenger
* @type {Object}
*/
think.messenger = messenger;
/**
* base controller class
*/
think.Controller = class Controller {
constructor(ctx) {
this.ctx = ctx;
}
};
/**
* base logic class
*/
think.Logic = class Logic extends think.Controller {};
const getClass = function(type, name, m) {
const mcls = think.app[type];
let cls = null;
if (think.app.modules.length) {
if (mcls[m]) {
cls = mcls[m][name];
}
if (!cls && m !== 'common' && mcls.common) {
cls = mcls.common[name];
}
} else {
cls = mcls[name];
}
return cls;
};
/**
* get controller instance
* @param {String} name
* @param {Object} ctx
* @param {String} m
*/
think.controller = (name, ctx, m = 'common') => {
const Cls = getClass('controllers', name, m);
assert(Cls, `can not find controller:${name}`);
return new Cls(ctx);
};
/**
* get service
*/
think.service = (name, m) => {
return getClass('services', name, m);
};
/**
* execute action
*/
think.action = (controller, actionName, ctx, m = 'common') => {
const Cls = getClass('controllers', controller, m);
assert(Cls, `can not find controller:${controller}`);
const instance = new Cls(ctx);
let promise = Promise.resolve();
if (instance.__before) {
promise = Promise.resolve(instance.__before())
}
return promise.then(data => {
if (data === false) return false;
let method = `${actionName}Action`;
if (!instance[method]) {
method = '__call';
}
if (instance[method]) {
return instance[method]();
}
}).then(data => {
if (data === false) return false;
if (instance.__after) {
instance.__after();
}
return data;
}).catch(e => {
console.log(e)
})
};
// before start server
const promises = [];
think.beforeStartServer = fn => {
if (fn) {
assert(helper.isFunction(fn), 'fn in think.beforeStartServer must be a function');
return promises.push(fn());
}
const promise = Promise.all(promises);
const timeout = helper.ms(think.config('startServerTimeout'));
const timeoutPromise = helper.timeout(timeout).then(() => {
const err = new Error(`waiting for start server timeout, time: ${timeout}ms`);
return Promise.reject(err);
});
return Promise.race([promise, timeoutPromise]);
};