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
120 lines (95 loc) · 4.57 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
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)
const path = require('path')
winston.level = 'debug'
if (cluster.isMaster) {
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).'))
}
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()
})
} 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.'))
})
/// /////////////////////////////////////////////////////////////////////
/// Various 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)
// Admin app
app.use('/admin', express.static(path.join(__dirname, '/node_modules/@speckle/speckle-admin')))
// Viewer app
app.use('/view', express.static(path.join(__dirname, '/node_modules/@speckle/speckle-viewer')))
// Prop until we get to proper plugin system
app.use('/', express.static(path.join(__dirname, '/node_modules/@speckle/speckle-admin')))
/// /////////////////////////////////////////////////////////////////////
/// 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/v1/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}.`))
})
}