Newer
Older
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
const express = require('express')
const { promisify } = require('util')
const http = require('http')
const config = require('config')
const fs = require('fs')
const path = require('path')
const logger = require('@pubsweet/logger')
let server
const startServer = async (app = express()) => {
if (server) return server
let configureApp
// ./server/app.js in your app is used if it exist,
// and no different entrypoint is configured in the
// config at `pubsweet-server.app`
const appPath = path.resolve('.', 'server', 'app.js')
if (config.has('pubsweet-server.app')) {
// See if a custom app entrypoint is configured
configureApp = config.get('pubsweet-server.app')
} else if (fs.existsSync(appPath)) {
// See if a custom app entrypoint exists at ./server/app.js
/* eslint-disable-next-line global-require, import/no-dynamic-require */
configureApp = require(appPath)
} else {
// If no custom entrypoints exist, use the default
/* eslint-disable-next-line global-require */
configureApp = require('./app')
}
const configuredApp = configureApp(app)
const port = config['pubsweet-server'].port || 3000
configuredApp.set('port', port)
const httpServer = http.createServer(configuredApp)
httpServer.app = configuredApp
logger.info(`Starting HTTP server`)
const startListening = promisify(httpServer.listen).bind(httpServer)
await startListening(port)
logger.info(`App is listening on port ${port}`)
await configuredApp.onListen(httpServer)
httpServer.originalClose = httpServer.close
httpServer.close = async cb => {
server = undefined
await configuredApp.onClose()
return httpServer.originalClose(cb)
}
server = httpServer
return httpServer
}
module.exports = startServer