Refactors configuration management (#4271)
* Adds flow types / Configuration interfaces * Lets call it options * Use a single interface to generate the configurations * Translates options to definitions only if comments are set * improves logic * Moves objects around * Fixes issue affecting logging of circular objects * fixes undefined env * Moves all defaults to defaults * Adds back CLI defaults * Restored defaults in commander.js * Merge provided defaults and platform defaults * Addresses visual nits * Improves Config.js code * Adds ability to pass the default value in trailing comments * Load platform defaults from the definitions file * proper default values on various options * Adds ParseServer.start and server.start(options) as quick startup methods * Moves creating liveQueryServer http into ParseServer.js * removes dead code * Adds tests to guarantee we can start a LQ Server from main module * Fixes incorrect code regading liveQuery init port * Start a http server for LQ if port is specified * ensure we dont fail if config.port is not set * Specify port * ignore other path skipped in tests * Adds test for custom middleware setting * Refactors new Config into Config.get - Hides AppCache from ParseServer.js, use Config.put which validates * Extracts controller creation into Controllers/index.js - This makes the ParseServer init way simpler * Move serverURL inference into ParseServer * review nits
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
/* eslint-disable no-console */
|
||||
import express from 'express';
|
||||
import ParseServer from '../index';
|
||||
import definitions from './definitions/parse-server';
|
||||
import cluster from 'cluster';
|
||||
import os from 'os';
|
||||
import runner from './utils/runner';
|
||||
const path = require("path");
|
||||
|
||||
const help = function(){
|
||||
console.log(' Get Started guide:');
|
||||
@@ -29,79 +27,12 @@ const help = function(){
|
||||
console.log('');
|
||||
};
|
||||
|
||||
function startServer(options, callback) {
|
||||
const app = express();
|
||||
if (options.middleware) {
|
||||
let middleware;
|
||||
if (typeof options.middleware == 'function') {
|
||||
middleware = options.middleware;
|
||||
} if (typeof options.middleware == 'string') {
|
||||
middleware = require(path.resolve(process.cwd(), options.middleware));
|
||||
} else {
|
||||
throw "middleware should be a string or a function";
|
||||
}
|
||||
app.use(middleware);
|
||||
}
|
||||
|
||||
const parseServer = new ParseServer(options);
|
||||
const sockets = {};
|
||||
app.use(options.mountPath, parseServer.app);
|
||||
|
||||
const server = app.listen(options.port, options.host, callback);
|
||||
server.on('connection', initializeConnections);
|
||||
|
||||
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
|
||||
let liveQueryServer = server;
|
||||
if (options.liveQueryPort) {
|
||||
liveQueryServer = express().listen(options.liveQueryPort, () => {
|
||||
console.log('ParseLiveQuery listening on ' + options.liveQueryPort);
|
||||
});
|
||||
}
|
||||
ParseServer.createLiveQueryServer(liveQueryServer, options.liveQueryServerOptions);
|
||||
}
|
||||
|
||||
function initializeConnections(socket) {
|
||||
/* Currently, express doesn't shut down immediately after receiving SIGINT/SIGTERM if it has client connections that haven't timed out. (This is a known issue with node - https://github.com/nodejs/node/issues/2642)
|
||||
|
||||
This function, along with `destroyAliveConnections()`, intend to fix this behavior such that parse server will close all open connections and initiate the shutdown process as soon as it receives a SIGINT/SIGTERM signal. */
|
||||
|
||||
const socketId = socket.remoteAddress + ':' + socket.remotePort;
|
||||
sockets[socketId] = socket;
|
||||
|
||||
socket.on('close', () => {
|
||||
delete sockets[socketId];
|
||||
});
|
||||
}
|
||||
|
||||
function destroyAliveConnections() {
|
||||
for (const socketId in sockets) {
|
||||
try {
|
||||
sockets[socketId].destroy();
|
||||
} catch (e) { /* */ }
|
||||
}
|
||||
}
|
||||
|
||||
const handleShutdown = function() {
|
||||
console.log('Termination signal received. Shutting down.');
|
||||
destroyAliveConnections();
|
||||
server.close();
|
||||
parseServer.handleShutdown();
|
||||
};
|
||||
process.on('SIGTERM', handleShutdown);
|
||||
process.on('SIGINT', handleShutdown);
|
||||
}
|
||||
|
||||
|
||||
runner({
|
||||
definitions,
|
||||
help,
|
||||
usage: '[options] <path/to/configuration.json>',
|
||||
start: function(program, options, logOptions) {
|
||||
if (!options.serverURL) {
|
||||
options.serverURL = `http://localhost:${options.port}${options.mountPath}`;
|
||||
}
|
||||
|
||||
if (!options.appId || !options.masterKey || !options.serverURL) {
|
||||
if (!options.appId || !options.masterKey) {
|
||||
program.outputHelp();
|
||||
console.error("");
|
||||
console.error('\u001b[31mERROR: appId and masterKey are required\u001b[0m');
|
||||
@@ -132,12 +63,12 @@ runner({
|
||||
cluster.fork();
|
||||
});
|
||||
} else {
|
||||
startServer(options, () => {
|
||||
ParseServer.start(options, () => {
|
||||
console.log('[' + process.pid + '] parse-server running on ' + options.serverURL);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
startServer(options, () => {
|
||||
ParseServer.start(options, () => {
|
||||
logOptions();
|
||||
console.log('');
|
||||
console.log('[' + process.pid + '] parse-server running on ' + options.serverURL);
|
||||
|
||||
Reference in New Issue
Block a user