Close server correctly & update fetch restart logic

This commit is contained in:
Aurelsicoko 2017-10-02 14:17:44 +02:00
parent 13f89d47c4
commit 69e4313abe
12 changed files with 122 additions and 47 deletions

View File

@ -46,13 +46,27 @@ function formatQueryParams(params) {
/**
* Server restart watcher
* @param response
* @returns {object} the response data
* @returns {object} the response data
*/
function serverRestartWatcher(response) {
return new Promise(resolve => {
setTimeout(() => {
resolve(response);
}, 3000);
return new Promise((resolve, reject) => {
fetch(`${Strapi.apiUrl}/_health`, {
method: 'HEAD',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
'Keep-Alive': false
}
})
.then(() => {
resolve(response);
})
.catch(err => {
setTimeout(() => {
return serverRestartWatcher(response)
.then(resolve);
}, 100);
});
});
}
@ -66,9 +80,9 @@ function serverRestartWatcher(response) {
*/
export default function request(url, options = {}, shouldWatchServerRestart = false) {
// Set headers
options.headers = {
options.headers = Object.assign({
'Content-Type': 'application/json',
};
}, options.headers);
// Add parameters to url
url = _.startsWith(url, '/')

View File

@ -60,7 +60,7 @@ export function* submitChanges() {
const requestUrl = method === 'POST' ? baseUrl : `${baseUrl}${body.name}`;
const opts = { method, body };
const response = yield call(request, requestUrl, opts, true);
if (response.ok) {
if (method === 'POST') {
storeData.clearAppStorage();

View File

@ -72,12 +72,17 @@ module.exports = function() {
setFilesToWatch(process.cwd());
if (cluster.isMaster) {
cluster.on('message', (worker, message) => {
switch (message) {
case 'reload':
strapi.log.info('The server is restarting\n');
_.forEach(cluster.workers, worker => worker.send('isKilled'));
break;
case 'kill':
_.forEach(cluster.workers, worker => worker.kill());
cluster.fork();
@ -95,8 +100,23 @@ module.exports = function() {
cluster.fork();
}
if (cluster.isWorker) return strapi.start(afterwards);
else return;
if (cluster.isWorker) {
process.on('message', (message) => {
switch (message) {
case 'isKilled':
strapi.server.destroy(() => {
process.send('kill');
});
break;
default:
// Do nothing.
}
});
return strapi.start(afterwards);
} else {
return;
}
}
// Otherwise, if no workable local `strapi` module exists,

View File

@ -135,21 +135,23 @@ class Strapi extends EventEmitter {
};
}
stop() {
stop(cb) {
// Destroy server and available connections.
this.server.destroy();
if (cluster.isWorker && process.env.NODE_ENV === 'development' && get(this.config, 'currentEnvironment.server.autoReload.enabled') === true) process.send('stop');
if (cluster.isWorker && process.env.NODE_ENV === 'development' && get(this.config, 'currentEnvironment.server.autoReload.enabled') === true) {
process.send('stop');
}
// Kill process.
process.exit(0);
}
async load() {
strapi.app.use(async (ctx, next) => {
this.app.use(async (ctx, next) => {
if (ctx.request.url === '/_health' && ctx.request.method === 'HEAD') {
ctx.set('strapi', 'You are so French !');
ctx.set('status', 204);
ctx.status = 204;
} else {
await next();
}

View File

@ -111,6 +111,7 @@ module.exports.app = async function() {
// These middlewares cannot be disabled.
merge(flattenMiddlewaresConfig, {
// Necessary middlewares for the core.
responses: {
enabled: true
},
@ -122,6 +123,16 @@ module.exports.app = async function() {
},
boom: {
enabled: true
},
// Necessary middlewares for the administration panel.
cors: {
enabled: true
},
xframe: {
enabled: true
},
xss: {
enabled: true
}
});

View File

@ -44,7 +44,7 @@ module.exports = strapi => {
? ctx.body || error && error.message
: Boom.wrap(error, ctx.status, ctx.body || error.message);
}
// Empty body is considered as `notFound` response.
if (!ctx.body) {
ctx.notFound();

View File

@ -47,17 +47,29 @@ module.exports = strapi => {
initialize: function(cb) {
strapi.app.use(
async (ctx, next) => {
if (ctx.request.admin) return next();
if (ctx.request.admin) {
return strapi.koaMiddlewares.kcors({
origin: '*',
exposeHeaders: this.defaults.cors.expose,
maxAge: this.defaults.cors.maxAge,
credentials: this.defaults.cors.credentials,
allowMethods: this.defaults.cors.methods,
allowHeaders: this.defaults.cors.headers,
keepHeadersOnError: this.defaults.cors.keepHeadersOnError
})(ctx, next);
} else if (strapi.config.currentEnvironment.security.cors.enabled) {
return strapi.koaMiddlewares.kcors({
origin: strapi.config.middleware.settings.cors.origin,
exposeHeaders: strapi.config.middleware.settings.cors.expose,
maxAge: strapi.config.middleware.settings.cors.maxAge,
credentials: strapi.config.middleware.settings.cors.credentials,
allowMethods: strapi.config.middleware.settings.cors.methods,
allowHeaders: strapi.config.middleware.settings.cors.headers,
keepHeadersOnError: strapi.config.middleware.settings.cors.keepHeadersOnError
})(ctx, next);
}
await strapi.koaMiddlewares.kcors({
origin: strapi.config.middleware.settings.cors.origin,
exposeHeaders: strapi.config.middleware.settings.cors.expose,
maxAge: strapi.config.middleware.settings.cors.maxAge,
credentials: strapi.config.middleware.settings.cors.credentials,
allowMethods: strapi.config.middleware.settings.cors.methods,
allowHeaders: strapi.config.middleware.settings.cors.headers,
keepHeadersOnError: strapi.config.middleware.settings.cors.keepHeadersOnError
})(ctx, next);
await next();
}
);

View File

@ -27,7 +27,7 @@ module.exports = strapi => {
initialize: function(cb) {
strapi.app.use(
async (ctx, next) => {
if (ctx.request.admin) return next();
if (ctx.request.admin) return await next();
strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.csp(strapi.config.middleware.settings.csp)

View File

@ -29,7 +29,7 @@ module.exports = strapi => {
initialize: function(cb) {
strapi.app.use(
async (ctx, next) => {
if (ctx.request.admin) return next();
if (ctx.request.admin) return await next();
await strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.csrf({

View File

@ -6,16 +6,14 @@ const { parallel } = require('async');
const { after, includes, indexOf, drop, dropRight, uniq, defaultsDeep, get, set, isEmpty, isUndefined, union } = require('lodash');
module.exports = function() {
const accepted = Object.keys(this.plugins).map(url => `^\/${url}`).concat(['^\/admin']);
// Set if is admin destination for middleware application.
// TODO: Use dynamic config for admin url.
strapi.app.use(async (ctx, next) => {
const accepted = Object.keys(strapi.plugins).map(url => `^\/${url}`).concat(['^\/admin']);
this.app.use(async (ctx, next) => {
ctx.request.admin = accepted.some(rx => new RegExp(rx).test(ctx.request.url));
strapi.app.use(async (ctx, next) => {
ctx.request.admin = accepted.some(rx => new RegExp(rx).test(ctx.request.url));
return next();
});
await next();
});
// Method to initialize middlewares and emit an event.

View File

@ -28,13 +28,22 @@ module.exports = strapi => {
initialize: function(cb) {
strapi.app.use(
async (ctx, next) => {
if (ctx.request.admin) return next();
if (ctx.request.admin) {
return strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xframe({
enabled: this.defaults.xframe.enabled,
value: this.defaults.xframe.value
})
)(ctx, next);
} else if (strapi.config.currentEnvironment.security.xframe.enabled) {
return strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xframe({
value: strapi.config.middleware.settings.xframe.value
})
)(ctx, next);
}
strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xframe({
value: strapi.config.middleware.settings.xframe.value
})
)(ctx, next);
await next();
}
);

View File

@ -28,14 +28,23 @@ module.exports = strapi => {
initialize: function(cb) {
strapi.app.use(
async (ctx, next) => {
if (ctx.request.admin) return next();
if (ctx.request.admin) {
return strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xssProtection({
enabled: true,
mode: this.defaults.xss.mode
})
)(ctx, next);
} else if (strapi.config.currentEnvironment.security.xss.enabled) {
strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xssProtection({
enabled: strapi.config.middleware.settings.xss.enabled,
mode: strapi.config.middleware.settings.xss.mode
})
)(ctx, next);
}
strapi.koaMiddlewares.convert(
strapi.koaMiddlewares.lusca.xssProtection({
enabled: strapi.config.middleware.settings.xss.enabled,
mode: strapi.config.middleware.settings.xss.mode
})
)(ctx, next);
await next();
}
);