510 lines
13 KiB
JavaScript
Raw Normal View History

2015-10-01 00:30:16 +02:00
'use strict';
2017-07-25 17:12:18 +02:00
const http = require('http');
const path = require('path');
const fse = require('fs-extra');
2018-05-04 17:02:27 +02:00
const Koa = require('koa');
2019-08-21 11:05:33 +02:00
const Router = require('koa-router');
2019-04-05 16:11:09 +02:00
const _ = require('lodash');
const chalk = require('chalk');
const CLITable = require('cli-table3');
const { models, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('@strapi/utils');
const { createLogger } = require('@strapi/logger');
2021-05-10 15:36:09 +02:00
const { Database } = require('@strapi/database');
const loadConfiguration = require('./core/app-configuration');
2018-05-04 17:02:27 +02:00
const utils = require('./utils');
const loadModules = require('./core/load-modules');
const bootstrap = require('./core/bootstrap');
2017-07-24 19:58:03 +02:00
const initializeMiddlewares = require('./middlewares');
const initializeHooks = require('./hooks');
const createStrapiFs = require('./core/fs');
2019-12-17 10:35:04 +01:00
const createEventHub = require('./services/event-hub');
2019-12-17 11:24:14 +01:00
const createWebhookRunner = require('./services/webhook-runner');
const { webhookModel, createWebhookStore } = require('./services/webhook-store');
2019-12-17 11:24:14 +01:00
const { createCoreStore, coreStoreModel } = require('./services/core-store');
const createEntityService = require('./services/entity-service');
const entityValidator = require('./services/entity-validator');
const createTelemetry = require('./services/metrics');
const createUpdateNotifier = require('./utils/update-notifier');
const ee = require('./utils/ee');
2019-12-17 10:35:04 +01:00
2021-02-16 12:23:51 +01:00
const LIFECYCLES = {
REGISTER: 'register',
BOOTSTRAP: 'bootstrap',
2021-02-16 12:23:51 +01:00
};
class Strapi {
2020-01-10 12:25:41 +01:00
constructor(opts = {}) {
2017-08-02 13:17:40 +02:00
this.reload = this.reload();
2016-07-06 15:51:52 +02:00
// Expose `koa`.
2016-11-04 16:00:19 +01:00
this.app = new Koa();
2019-08-21 11:05:33 +02:00
this.router = new Router();
2016-07-06 15:51:52 +02:00
this.initServer();
2017-09-12 17:58:31 +02:00
// Utils.
this.utils = {
2018-05-18 14:22:24 +02:00
models,
2017-09-12 17:58:31 +02:00
};
2019-12-17 10:35:04 +01:00
this.dir = opts.dir || process.cwd();
this.admin = {};
this.plugins = {};
this.config = loadConfiguration(this.dir, opts);
this.app.proxy = this.config.get('server.proxy');
// Logger.
const loggerUserConfiguration = this.config.get('logger', {});
this.log = createLogger(loggerUserConfiguration);
this.isLoaded = false;
2019-12-17 10:35:04 +01:00
// internal services.
this.fs = createStrapiFs(this);
this.eventHub = createEventHub();
this.requireProjectBootstrap();
createUpdateNotifier(this).notify();
2019-12-17 10:35:04 +01:00
}
get EE() {
return ee({ dir: this.dir, logger: this.log });
}
handleRequest(req, res) {
if (!this.requestHandler) {
this.requestHandler = this.app.callback();
}
return this.requestHandler(req, res);
}
requireProjectBootstrap() {
const bootstrapPath = path.resolve(this.dir, 'config/functions/bootstrap.js');
if (fse.existsSync(bootstrapPath)) {
require(bootstrapPath);
}
}
logStats() {
const columns = Math.min(process.stderr.columns, 80) - 2;
console.log();
console.log(chalk.black.bgWhite(_.padEnd(' Project information', columns)));
console.log();
const infoTable = new CLITable({
colWidths: [20, 50],
chars: { mid: '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' },
});
const isEE = strapi.EE === true && ee.isEE === true;
infoTable.push(
[chalk.blue('Time'), `${new Date()}`],
[chalk.blue('Launched in'), Date.now() - this.config.launchedAt + ' ms'],
[chalk.blue('Environment'), this.config.environment],
[chalk.blue('Process PID'), process.pid],
[chalk.blue('Version'), `${this.config.info.strapi} (node ${process.version})`],
[chalk.blue('Edition'), isEE ? 'Enterprise' : 'Community']
);
console.log(infoTable.toString());
console.log();
console.log(chalk.black.bgWhite(_.padEnd(' Actions available', columns)));
console.log();
}
logFirstStartupMessage() {
this.logStats();
console.log(chalk.bold('One more thing...'));
console.log(
chalk.grey('Create your first administrator 💻 by going to the administration panel at:')
);
console.log();
const addressTable = new CLITable();
const adminUrl = getAbsoluteAdminUrl(strapi.config);
addressTable.push([chalk.bold(adminUrl)]);
console.log(`${addressTable.toString()}`);
console.log();
}
logStartupMessage() {
this.logStats();
console.log(chalk.bold('Welcome back!'));
if (this.config.serveAdminPanel === true) {
console.log(chalk.grey('To manage your project 🚀, go to the administration panel at:'));
const adminUrl = getAbsoluteAdminUrl(strapi.config);
console.log(chalk.bold(adminUrl));
console.log();
}
console.log(chalk.grey('To access the server ⚡️, go to:'));
const serverUrl = getAbsoluteServerUrl(strapi.config);
console.log(chalk.bold(serverUrl));
console.log();
}
initServer() {
this.server = http.createServer(this.handleRequest.bind(this));
2019-04-05 16:11:09 +02:00
// handle port in use cleanly
this.server.on('error', err => {
if (err.code === 'EADDRINUSE') {
return this.stopWithError(`The port ${err.port} is already used by another application.`);
2019-04-05 16:11:09 +02:00
}
this.log.error(err);
});
// Close current connections to fully destroy the server
2017-07-25 17:12:18 +02:00
const connections = {};
2017-07-25 17:12:18 +02:00
this.server.on('connection', conn => {
const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn;
2016-07-26 11:57:50 +02:00
conn.on('close', function() {
2018-05-04 17:02:27 +02:00
delete connections[key];
2017-07-25 17:12:18 +02:00
});
});
2016-07-26 11:57:50 +02:00
2017-07-25 17:12:18 +02:00
this.server.destroy = cb => {
this.server.close(cb);
2016-07-26 11:57:50 +02:00
2017-07-25 17:12:18 +02:00
for (let key in connections) {
connections[key].destroy();
2018-05-04 17:02:27 +02:00
}
2017-07-25 17:12:18 +02:00
};
}
async start(cb) {
try {
if (!this.isLoaded) {
await this.load();
}
this.app.use(this.router.routes()).use(this.router.allowedMethods());
// Launch server.
this.listen(cb);
} catch (err) {
this.stopWithError(err);
}
}
async destroy() {
if (_.has(this, 'server.destroy')) {
await new Promise(res => this.server.destroy(res));
}
2020-12-29 17:44:14 +01:00
await Promise.all(
Object.values(this.plugins).map(plugin => {
if (_.has(plugin, 'destroy') && typeof plugin.destroy === 'function') {
return plugin.destroy();
}
})
);
if (_.has(this, 'admin')) {
await this.admin.destroy();
}
this.eventHub.removeAllListeners();
if (_.has(this, 'db')) {
await this.db.destroy();
}
this.telemetry.destroy();
delete global.strapi;
}
/**
* Add behaviors to the server
*/
async listen(cb) {
const onListen = async err => {
if (err) return this.stopWithError(err);
// Is the project initialised?
const isInitialised = await utils.isInitialised(this);
// Should the startup message be displayed?
const hideStartupMessage = process.env.STRAPI_HIDE_STARTUP_MESSAGE
? process.env.STRAPI_HIDE_STARTUP_MESSAGE === 'true'
: false;
if (hideStartupMessage === false) {
if (!isInitialised) {
this.logFirstStartupMessage();
} else {
this.logStartupMessage();
}
}
// Get database clients
const databaseClients = _.map(this.config.get('connections'), _.property('settings.client'));
// Emit started event.
await this.telemetry.send('didStartServer', {
database: databaseClients,
plugins: this.config.installedPlugins,
providers: this.config.installedProviders,
});
if (cb && typeof cb === 'function') {
cb();
}
2021-06-28 12:34:29 +02:00
// if (
// (this.config.environment === 'development' &&
// this.config.get('server.admin.autoOpen', true) !== false) ||
// !isInitialised
// ) {
// await utils.openBrowser.call(this);
// }
};
const listenSocket = this.config.get('server.socket');
const listenErrHandler = err => onListen(err).catch(err => this.stopWithError(err));
if (listenSocket) {
this.server.listen(listenSocket, listenErrHandler);
} else {
this.server.listen(
this.config.get('server.port'),
this.config.get('server.host'),
listenErrHandler
);
}
2016-07-26 11:57:50 +02:00
}
stopWithError(err, customMessage) {
2021-06-17 19:51:35 +02:00
console.log(err);
2019-04-05 16:11:09 +02:00
this.log.debug(`⛔️ Server wasn't able to start properly.`);
if (customMessage) {
this.log.error(customMessage);
}
2021-06-17 16:17:15 +02:00
2019-04-05 16:11:09 +02:00
this.log.error(err);
return this.stop();
}
stop(exitCode = 1) {
2017-07-25 17:12:18 +02:00
// Destroy server and available connections.
if (_.has(this, 'server.destroy')) {
this.server.destroy();
}
2017-09-04 15:38:29 +02:00
2019-05-13 17:32:52 +02:00
if (this.config.autoReload) {
process.send('stop');
}
2017-09-04 15:38:29 +02:00
// Kill process
2021-07-05 18:35:16 +02:00
// process.exit(exitCode);
2016-07-26 11:57:50 +02:00
}
2017-07-24 19:58:03 +02:00
async load() {
this.app.use(async (ctx, next) => {
if (ctx.request.url === '/_health' && ['HEAD', 'GET'].includes(ctx.request.method)) {
ctx.set('strapi', 'You are so French!');
ctx.status = 204;
2017-09-06 11:06:18 +02:00
} else {
await next();
}
});
const modules = await loadModules(this);
this.api = modules.api;
this.admin = modules.admin;
this.components = modules.components;
this.plugins = modules.plugins;
this.middleware = modules.middlewares;
this.hook = modules.hook;
2019-04-11 09:32:16 +02:00
await bootstrap(this);
2020-01-10 12:42:57 +01:00
// init webhook runner
this.webhookRunner = createWebhookRunner({
eventHub: this.eventHub,
logger: this.log,
configuration: this.config.get('server.webhooks', {}),
2020-01-10 12:42:57 +01:00
});
2018-04-24 12:30:43 +02:00
// Init core store
2021-06-17 16:17:15 +02:00
const contentTypes = [
2021-06-22 17:13:11 +02:00
// todo: move corestore and webhook to real models instead of content types to avoid adding extra attributes
2021-05-17 16:34:19 +02:00
coreStoreModel,
webhookModel,
...Object.values(strapi.models),
...Object.values(strapi.components),
...Object.values(strapi.admin.models),
...Object.values(strapi.plugins).flatMap(plugin => Object.values(plugin.models)),
...Object.values(strapi.api).flatMap(api => Object.values(api.models)),
];
2021-06-28 12:34:29 +02:00
// TODO: create in RootProvider
this.db = await Database.init({
2021-05-17 16:34:19 +02:00
...this.config.get('database'),
2021-06-17 16:17:15 +02:00
models: Database.transformContentTypes(contentTypes),
2021-05-17 16:34:19 +02:00
});
2021-02-12 12:52:29 +01:00
2021-07-05 18:35:16 +02:00
await this.db.schema.sync();
2021-05-17 16:34:19 +02:00
2021-02-16 12:23:51 +01:00
await this.runLifecyclesFunctions(LIFECYCLES.REGISTER);
2021-06-17 16:17:15 +02:00
// await this.db.initialize();
2019-09-20 12:44:24 +02:00
2019-12-17 11:24:14 +01:00
this.store = createCoreStore({
environment: this.config.environment,
db: this.db,
});
this.webhookStore = createWebhookStore({ db: this.db });
2019-12-17 20:59:57 +01:00
await this.startWebhooks();
this.entityValidator = entityValidator;
this.entityService = createEntityService({
db: this.db,
eventHub: this.eventHub,
entityValidator: this.entityValidator,
});
this.telemetry = createTelemetry(this);
2017-07-25 17:12:18 +02:00
// Initialize hooks and middlewares.
2019-08-21 11:05:33 +02:00
await initializeMiddlewares.call(this);
await initializeHooks.call(this);
2021-02-16 12:23:51 +01:00
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
await this.freeze();
this.isLoaded = true;
return this;
2016-07-26 11:57:50 +02:00
}
2019-12-17 20:59:57 +01:00
async startWebhooks() {
const webhooks = await this.webhookStore.findWebhooks();
webhooks.forEach(webhook => this.webhookRunner.add(webhook));
2019-12-17 20:59:57 +01:00
}
2017-08-02 11:25:18 +02:00
reload() {
2018-03-28 20:13:09 +02:00
const state = {
shouldReload: 0,
2018-03-28 20:13:09 +02:00
};
const reload = function() {
2018-08-31 13:47:10 +02:00
if (state.shouldReload > 0) {
// Reset the reloading state
2018-08-31 13:47:10 +02:00
state.shouldReload -= 1;
reload.isReloading = false;
2018-03-28 20:13:09 +02:00
return;
}
2019-05-13 17:32:52 +02:00
if (this.config.autoReload) {
2019-03-11 10:42:43 +01:00
this.server.close();
2018-01-04 16:03:34 +01:00
process.send('reload');
}
2017-08-02 11:25:18 +02:00
};
2018-03-28 20:13:09 +02:00
Object.defineProperty(reload, 'isWatching', {
configurable: true,
enumerable: true,
2018-05-18 14:22:24 +02:00
set: value => {
2018-03-28 20:13:09 +02:00
// Special state when the reloader is disabled temporarly (see GraphQL plugin example).
2018-08-31 13:47:10 +02:00
if (state.isWatching === false && value === true) {
state.shouldReload += 1;
}
2018-03-28 20:13:09 +02:00
state.isWatching = value;
},
get: () => {
return state.isWatching;
2018-05-18 14:22:24 +02:00
},
2018-03-28 20:13:09 +02:00
});
2017-08-02 11:25:18 +02:00
reload.isReloading = false;
reload.isWatching = true;
return reload;
2016-07-26 11:57:50 +02:00
}
2021-02-16 12:23:51 +01:00
async runLifecyclesFunctions(lifecycleName) {
const execLifecycle = async fn => {
if (!fn) {
return;
}
2019-08-12 15:35:40 +02:00
return fn();
};
2019-08-12 15:35:40 +02:00
2021-02-16 12:23:51 +01:00
const configPath = `functions.${lifecycleName}`;
2021-02-15 11:38:50 +01:00
2021-02-16 12:23:51 +01:00
// plugins
await Promise.all(
Object.keys(this.plugins).map(plugin => {
const pluginFunc = _.get(this.plugins[plugin], `config.${configPath}`);
return execLifecycle(pluginFunc).catch(err => {
strapi.log.error(`${lifecycleName} function in plugin "${plugin}" failed`);
2021-06-17 19:51:35 +02:00
console.error(err);
2021-02-16 12:23:51 +01:00
strapi.stop();
});
})
);
2019-04-05 16:11:09 +02:00
2021-02-16 12:23:51 +01:00
// user
await execLifecycle(_.get(this.config, configPath));
2021-02-16 12:23:51 +01:00
// admin
2021-02-15 11:38:50 +01:00
const adminFunc = _.get(this.admin.config, configPath);
2021-02-16 12:23:51 +01:00
return execLifecycle(adminFunc).catch(err => {
strapi.log.error(`${lifecycleName} function in admin failed`);
2021-06-17 19:51:35 +02:00
console.error(err);
strapi.stop();
});
2016-07-26 11:57:50 +02:00
}
2017-07-31 11:35:57 +02:00
async freeze() {
Object.freeze(this.config);
Object.freeze(this.dir);
Object.freeze(this.admin);
Object.freeze(this.plugins);
Object.freeze(this.api);
2016-07-26 11:57:50 +02:00
}
2021-06-28 22:37:19 +02:00
getModel(uid) {
2021-07-01 13:52:01 +02:00
return this.contentTypes[uid] || this.components[uid];
}
2019-05-16 21:37:45 +02:00
/**
* Binds queries with a specific model
2021-06-30 20:00:03 +02:00
* @param {string} uid
* @returns {}
2019-05-16 21:37:45 +02:00
*/
2021-06-30 20:00:03 +02:00
query(uid) {
return this.db.query(uid);
}
2016-07-26 11:57:50 +02:00
}
2019-04-11 09:32:16 +02:00
module.exports = options => {
const strapi = new Strapi(options);
global.strapi = strapi;
return strapi;
};
2021-06-30 20:00:03 +02:00
module.exports.Strapi = Strapi;