2015-10-01 00:30:16 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const path = require('path');
|
2016-04-21 11:39:18 +02:00
|
|
|
const http = require('http');
|
2016-07-06 15:51:52 +02:00
|
|
|
const EventEmitter = require('events').EventEmitter;
|
2015-10-01 00:30:16 +02:00
|
|
|
|
|
|
|
// Local dependencies.
|
2016-11-04 16:00:19 +01:00
|
|
|
const Koa = require('koa');
|
2015-10-01 00:30:16 +02:00
|
|
|
const mixinAfter = require('./private/after');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an Strapi instance.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
|
2017-04-07 16:54:32 +02:00
|
|
|
// Private properties
|
2016-07-26 11:57:50 +02:00
|
|
|
let _instance = null;
|
|
|
|
|
|
|
|
class Strapi extends EventEmitter {
|
2016-07-06 15:51:52 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2016-07-26 11:57:50 +02:00
|
|
|
// Singleton
|
|
|
|
if (!_instance) {
|
|
|
|
_instance = this;
|
|
|
|
}
|
|
|
|
|
2016-07-06 15:51:52 +02:00
|
|
|
// Remove memory-leak warning about max listeners.
|
|
|
|
this.setMaxListeners(0);
|
|
|
|
|
|
|
|
// Mixin support for `Strapi.prototype.after()`.
|
|
|
|
mixinAfter(this);
|
|
|
|
|
|
|
|
// Expose `koa`.
|
2016-11-04 16:00:19 +01:00
|
|
|
this.app = new Koa();
|
2016-07-06 15:51:52 +02:00
|
|
|
|
|
|
|
// Mount the HTTP server.
|
2016-09-28 11:27:24 +02:00
|
|
|
this.server = new http.Server(this.app.callback());
|
2016-07-06 15:51:52 +02:00
|
|
|
|
|
|
|
// Expose every middleware inside `strapi.middlewares`.
|
|
|
|
this.middlewares = require('koa-load-middlewares')({
|
|
|
|
config: path.resolve(__dirname, '..', 'package.json'),
|
2016-11-07 16:31:34 +01:00
|
|
|
pattern: ['koa-*', 'koa.*', 'k*'],
|
2016-07-06 15:51:52 +02:00
|
|
|
scope: ['dependencies', 'devDependencies'],
|
|
|
|
replaceString: /^koa(-|\.)/,
|
2016-08-11 12:42:00 +02:00
|
|
|
camelize: true,
|
2016-11-07 16:31:34 +01:00
|
|
|
lazy: false
|
2016-07-06 15:51:52 +02:00
|
|
|
});
|
|
|
|
|
2017-02-01 14:48:30 +01:00
|
|
|
this.connections = {};
|
|
|
|
|
2016-07-06 15:51:52 +02:00
|
|
|
// New Winston logger.
|
|
|
|
this.log = require('strapi-utils').logger;
|
2016-07-26 11:57:50 +02:00
|
|
|
|
|
|
|
return _instance;
|
2016-07-06 15:51:52 +02:00
|
|
|
}
|
2016-07-26 11:57:50 +02:00
|
|
|
|
|
|
|
// Method to initialize instance
|
|
|
|
initialize(cb) {
|
|
|
|
require('./private/initialize').apply(this, [cb]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to load instance
|
|
|
|
load(configOverride, cb) {
|
|
|
|
require('./load').apply(this, [configOverride, cb]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to start instance
|
|
|
|
start(configOverride, cb) {
|
|
|
|
require('./start').apply(this, [configOverride, cb]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to stop instance
|
|
|
|
stop() {
|
|
|
|
require('./stop').apply(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to expose instance globals
|
|
|
|
exposeGlobals(cb) {
|
|
|
|
require('./private/exposeGlobals').apply(this, [cb]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to run instance bootstrap
|
|
|
|
runBootstrap(cb) {
|
|
|
|
require('./private/bootstrap').apply(this, [cb]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to verify strapi dependency
|
|
|
|
isLocalStrapiValid(strapiPath, appPath) {
|
|
|
|
require('./private/isLocalStrapiValid').apply(this, [strapiPath, appPath]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to verify strapi application
|
|
|
|
isStrapiAppSync(appPath) {
|
|
|
|
require('./private/isStrapiAppSync').apply(this, [appPath]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new Strapi();
|