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
/
server.js
165 lines (126 loc) · 5.36 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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 mongoose = require( 'mongoose' ).set( 'debug', false )
const expressWinston = require( 'express-winston' )
const redis = require( 'redis' )
const logger = require( './config/logger' )
const URL = require( 'url' ).URL
// load up .env
const configResult = require( 'dotenv' ).config( { path: './.env' } )
process.env.CANONICAL_URL = new URL( process.env.CANONICAL_URL ).origin
if ( configResult.error ) {
logger.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 ) {
logger.info( chalk.blue( `
█▀▀ █▀▀█ █▀▀ █▀▀▀ █ █ █ █▀▀
▀▀█ █ █ █▀▀ █ █▀▄ █ █▀▀
▀▀▀ █▀▀▀ ▀▀▀ ▀▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀
` ) + `
█
█ https://speckle.works
█ The Open Source Data Platform for AEC.
█
` +
chalk.red( `
█ Server running at: ${process.env.CANONICAL_URL}
` )
)
logger.level = 'debug'
let osCpus = require( 'os' ).cpus( ).length
let envCpus = process.env.MAX_PROC
let numWorkers = envCpus ? ( envCpus > osCpus ? osCpus : envCpus ) : osCpus
logger.debug( `Setting up ${numWorkers} workers.\n` )
for ( let i = 0; i < numWorkers; i++ ) { cluster.fork( ) }
cluster.on( 'online', worker => {
logger.debug( `Speckle worker ${worker.process.pid} is now online.\n` )
} )
cluster.on( 'exit', ( worker, code, signal ) => {
logger.debug( `Speckle worker ${worker.process.pid} just died with code ${code} and signal ${signal}.` )
logger.debug( `Starting a new one...` )
cluster.fork( )
} )
// flush redis
let redisClient = redis.createClient( process.env.REDIS_URL )
redisClient.on( 'connect', ( ) => {
logger.debug( `Flushing redis database.` )
redisClient.flushdb( )
} )
require( './app/telemetry/initTelemetry' )( )
/////////////////////////////////////////////////////////////////////////
/// CHILD processes /////.
/////////////////////////////////////////////////////////////////////////
} else {
// Express inits
var app = express( )
app.use( cors( ) ) // allow cors
app.use( expressWinston.logger( {
winstonInstance: logger,
colorize: true,
msg: 'HTTP {{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms'
} ) )
// Mongo handlers
mongoose.Promise = global.Promise
mongoose.connect( process.env.MONGODB_URI, { useNewUrlParser: true, autoReconnect: true, reconnectTries: 5, keepAlive: 10 }, ( err ) => {
if ( err ) throw err
else logger.debug( 'connected to mongoose at ' + process.env.MONGODB_URI )
} )
mongoose.connection.on( 'error', err => {
logger.debug( 'Failed to connect to DB ' + process.env.MONGODB_URI + ' on startup ', err )
} )
// When the connection is disconnected
mongoose.connection.on( 'disconnected', ( ) => {
logger.debug( 'Mongoose default was disconnected' )
} )
mongoose.connection.on( 'connected', ( ) => {
logger.debug( 'Connected to mongo.' )
} )
// 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( ) )
// Telemetry
require( './app/telemetry/appTelemetry' )( app )
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( plugin.serveSource ? plugin.serveSource : plugin.sourceDir ) ) )
} )
// expose an api
app.use( '/api/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', plugins )
require( './app/api/index' )( app, express, '/api/v1', plugins )
// init default register/login routes
require( './app/auth/index' )( app )
/// /////////////////////////////////////////////////////////////////////
/// LAUNCH /////.
/// /////////////////////////////////////////////////////////////////////
var port = process.env.PORT || 3000
var ip = process.env.IP || null
server.listen( port, ip, ( ) => {
logger.debug( `Speckle worker process ${process.pid} now running on port ${port}.` )
} )
}