BVB Source Codes

parse-server Show parse-server.js Source code

Return Download parse-server: download parse-server.js Source code - Download parse-server Source code - Type:.js
  1. /* eslint-disable no-console */
  2. import express from 'express';
  3. import ParseServer from '../index';
  4. import definitions from './definitions/parse-server';
  5. import cluster from 'cluster';
  6. import os from 'os';
  7. import runner from './utils/runner';
  8. const path = require("path");
  9.  
  10. const help = function(){
  11.   console.log('  Get Started guide:');
  12.   console.log('');
  13.   console.log('    Please have a look at the get started guide!');
  14.   console.log('    https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide');
  15.   console.log('');
  16.   console.log('');
  17.   console.log('  Usage with npm start');
  18.   console.log('');
  19.   console.log('    $ npm start -- path/to/config.json');
  20.   console.log('    $ npm start -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
  21.   console.log('    $ npm start -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
  22.   console.log('');
  23.   console.log('');
  24.   console.log('  Usage:');
  25.   console.log('');
  26.   console.log('    $ parse-server path/to/config.json');
  27.   console.log('    $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
  28.   console.log('    $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
  29.   console.log('');
  30. };
  31.  
  32. function startServer(options, callback) {
  33.   const app = express();
  34.   if (options.middleware) {
  35.     let middleware;
  36.     if (typeof options.middleware == 'function') {
  37.       middleware = options.middleware;
  38.     } if (typeof options.middleware == 'string') {
  39.       middleware = require(path.resolve(process.cwd(), options.middleware));
  40.     } else {
  41.       throw "middleware should be a string or a function";
  42.     }
  43.     app.use(middleware);
  44.   }
  45.  
  46.   const parseServer = new ParseServer(options);
  47.   const sockets = {};
  48.   app.use(options.mountPath, parseServer.app);
  49.  
  50.   const server = app.listen(options.port, options.host, callback);
  51.   server.on('connection', initializeConnections);
  52.  
  53.   if (options.startLiveQueryServer || options.liveQueryServerOptions) {
  54.     let liveQueryServer = server;
  55.     if (options.liveQueryPort) {
  56.       liveQueryServer = express().listen(options.liveQueryPort, () => {
  57.         console.log('ParseLiveQuery listening on ' + options.liveQueryPort);
  58.       });
  59.     }
  60.     ParseServer.createLiveQueryServer(liveQueryServer, options.liveQueryServerOptions);
  61.   }
  62.  
  63.   function initializeConnections(socket) {
  64.     /* 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)
  65.  
  66.       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. */
  67.  
  68.     const socketId = socket.remoteAddress + ':' + socket.remotePort;
  69.     sockets[socketId] = socket;
  70.  
  71.     socket.on('close', () => {
  72.       delete sockets[socketId];
  73.     });
  74.   }
  75.  
  76.   function destroyAliveConnections() {
  77.     for (const socketId in sockets) {
  78.       try {
  79.         sockets[socketId].destroy();
  80.       } catch (e) { /* */ }
  81.     }
  82.   }
  83.  
  84.   const handleShutdown = function() {
  85.     console.log('Termination signal received. Shutting down.');
  86.     destroyAliveConnections();
  87.     server.close();
  88.     parseServer.handleShutdown();
  89.   };
  90.   process.on('SIGTERM', handleShutdown);
  91.   process.on('SIGINT', handleShutdown);
  92. }
  93.  
  94.  
  95. runner({
  96.   definitions,
  97.   help,
  98.   usage: '[options] <path/to/configuration.json>',
  99.   start: function(program, options, logOptions) {
  100.     if (!options.serverURL) {
  101.       options.serverURL = `http://localhost:${options.port}${options.mountPath}`;
  102.     }
  103.  
  104.     if (!options.appId || !options.masterKey || !options.serverURL) {
  105.       program.outputHelp();
  106.       console.error("");
  107.       console.error('\u001b[31mERROR: appId and masterKey are required\u001b[0m');
  108.       console.error("");
  109.       process.exit(1);
  110.     }
  111.  
  112.     if (options["liveQuery.classNames"]) {
  113.       options.liveQuery = options.liveQuery || {};
  114.       options.liveQuery.classNames = options["liveQuery.classNames"];
  115.       delete options["liveQuery.classNames"];
  116.     }
  117.     if (options["liveQuery.redisURL"]) {
  118.       options.liveQuery = options.liveQuery || {};
  119.       options.liveQuery.redisURL = options["liveQuery.redisURL"];
  120.       delete options["liveQuery.redisURL"];
  121.     }
  122.  
  123.     if (options.cluster) {
  124.       const numCPUs = typeof options.cluster === 'number' ? options.cluster : os.cpus().length;
  125.       if (cluster.isMaster) {
  126.         logOptions();
  127.         for(let i = 0; i < numCPUs; i++) {
  128.           cluster.fork();
  129.         }
  130.         cluster.on('exit', (worker, code) => {
  131.           console.log(`worker ${worker.process.pid} died (${code})... Restarting`);
  132.           cluster.fork();
  133.         });
  134.       } else {
  135.         startServer(options, () => {
  136.           console.log('[' + process.pid + '] parse-server running on ' + options.serverURL);
  137.         });
  138.       }
  139.     } else {
  140.       startServer(options, () => {
  141.         logOptions();
  142.         console.log('');
  143.         console.log('[' + process.pid + '] parse-server running on ' + options.serverURL);
  144.       });
  145.     }
  146.   }
  147. });
  148.  
  149. /* eslint-enable no-console */
  150.  
downloadparse-server.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top