This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathserver.js
124 lines (98 loc) · 4.43 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const path = require( 'path' )
const cluster = require( 'cluster' )
const express = require( 'express' )
const cors = require( 'cors' )
const bodyParser = require( 'body-parser' )
const passport = require( 'passport' )
const chalk = require( 'chalk' )
const winston = require( 'winston' )
const expressWinston = require( 'express-winston' )
const mongoose = require( 'mongoose' ).set( 'debug', false )
// load up .env
const configResult = require( 'dotenv' ).config( { path: './.env' } )
if ( configResult.error ) {
winston.debug( chalk.bgRed( 'There is an error in the .env configuration file. Will use the default provided ones (if any).' ) )
}
// front-end plugins discovery registration
const plugins = require( './plugins' )( )
/////////////////////////////////////////////////////////////////////////
/// MASTER process /////.
/////////////////////////////////////////////////////////////////////////
if ( cluster.isMaster ) {
winston.level = 'debug'
winston.info( chalk.bgBlue( `Speckle is starting up.` ) )
let osCpus = require( 'os' ).cpus( ).length
let envCpus = process.env.MAX_PROC
let numWorkers = envCpus ? ( envCpus > osCpus ? osCpus : envCpus ) : osCpus
winston.debug( `Setting up ${numWorkers} workers.` )
for ( let i = 0; i < numWorkers; i++ ) { cluster.fork( ) }
cluster.on( 'online', worker => {
winston.debug( `Speckle worker ${worker.process.pid} is now online.` )
} )
cluster.on( 'exit', ( worker, code, signal ) => {
winston.debug( `Speckle worker ${worker.process.pid} just died with code ${code} and signal ${signal}.` )
winston.debug( `Starting a new one...` )
cluster.fork( )
} )
/////////////////////////////////////////////////////////////////////////
/// CHILD processes /////.
/////////////////////////////////////////////////////////////////////////
} else {
// Mongo handlers
mongoose.Promise = global.Promise
mongoose.connect( process.env.MONGODB_URI, { autoReconnect: true, reconnectTries: 5, keepAlive: 10 }, ( err ) => {
if ( err ) throw err
else winston.debug( 'connected to mongoose at ' + process.env.MONGODB_URI )
} )
mongoose.connection.on( 'error', err => {
winston.debug( 'Failed to connect to DB ' + process.env.MONGODB_URI + ' on startup ', err )
} )
// When the connection is disconnected
mongoose.connection.on( 'disconnected', ( ) => {
winston.debug( 'Mongoose default was disconnected' )
} )
mongoose.connection.on( 'connected', ( ) => {
winston.debug( chalk.red( 'Connected to mongo.' ) )
} )
// Express inits
var app = express( )
app.use( cors( ) ) // allow cors
app.use( expressWinston.logger( {
transports: [ new winston.transports.Console( { json: false, colorize: true, timestamp: true } ) ],
meta: false,
msg: 'HTTP {{req.method}} {{req.url}} {{res.statusCode}} '
} ) )
// throws a 413 if over REQ_SIZE
app.use( bodyParser.json( { limit: process.env.REQ_SIZE } ) )
app.use( bodyParser.urlencoded( { extended: true } ) )
app.use( passport.initialize( ) )
if ( process.env.INDENT_RESPONSES === 'true' ) { app.set( 'json spaces', 2 ) }
if ( process.env.EXPOSE_EMAILS === 'true' ) { app.enable( 'expose emails' ) }
require( './config/passport' )( passport )
// register plugins with express
plugins.forEach( plugin => {
app.use( plugin.serveFrom, express.static( path.join( __dirname, plugin.sourceDir ) ) )
} )
// expose an api
app.use( '/plugins', ( req, res ) => res.json( plugins ) )
// Websockets & HTTP Servers
var http = require( 'http' )
var server = http.createServer( app )
var WebSocketServer = require( 'ws' ).Server
var wss = new WebSocketServer( {
server: server
} )
require( './app/ws/SpeckleSockets' )( wss )
// Routes
// handle api versions gracefully
app.use( '/api/v0', ( req, res ) => res.status( 410 ).json( { error: 'The v0 API has been removed' } ) )
require( './app/api/index' )( app, express, '/api' )
require( './app/api/index' )( app, express, '/api/v1' )
/// /////////////////////////////////////////////////////////////////////
/// LAUNCH /////.
/// /////////////////////////////////////////////////////////////////////
var port = process.env.PORT || 3000
server.listen( port, ( ) => {
winston.debug( chalk.yellow( `Speckle worker process ${process.pid} now running on port ${port}.` ) )
} )
}