-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (124 loc) · 3.54 KB
/
index.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
const express = require('express')
const compression = require('compression')
const argv = require('minimist')(process.argv.slice(2))
const path = require('path')
const open = require('open')
const network = require('./lib/network.js')
const portManager = require('./lib/port-manager.js')
const timeout = require('./lib/timeout.js')
const debug = require('@ff0000-ad-tech/debug')
var log = debug('wp-creative-server')
// determine IP
global.serveIp = network.getIp()
// will increase sequentially until available ip/port is found
global.servePort = 5200
// set once the server is running
global.origin
global.app
global.api
// set app-path
global.appPath = __dirname
// set creative-path with --context,
// ex: `node ./node_modules/@ff0000-ad-tech/wp-creative-server/index.js --context ./`
global.servePath = path.resolve('context' in argv ? argv.context : global.appPath)
log(`Requested --context ${argv.context}`)
log(` Serve-path: ${global.servePath}`)
// cwd, by default is the creative-path
global.cwd = global.servePath
if (argv.cwd) {
log(`Specified --cwd`)
global.cwd = argv.cwd
}
log(` Working directory is: ${global.cwd}`)
// wp-deploy-manager (webpack.config.js) location, by default is the creative-path
global.wpDmPath = global.servePath
if (argv.wpDmPath) {
log(`Specified --wpDmPath`)
global.wpDmPath = argv.wpDmPath
}
log(` Deploy-Manager (webpack.config.js) at: ${global.wpDmPath}`)
/* -- Setup -----------------------------------------------
*
*
*
*/
var app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(compression())
/* -- STATE ----------------------------------------------
*
*
*
*/
var state = require('./lib/state.js')
state.reset()
/* -- Backend API ----------------------------------------------
*
*
*
*/
var backendApi = require('./routes/backend-api.js')
backendApi.api.getAppMeta(() => { })
backendApi.api.getPlugins(() => { })
backendApi.api.getCreative(() => { })
backendApi.api.readTargets(() => { })
backendApi.api.getProfiles(() => { })
backendApi.api.refreshTargets(() => { })
/* -- ROUTES ----------------------------------------------
*
*
*
*/
require('./routes/app')(app, express)
require('./routes/api')(app, express)
require('./routes/plugins')(app, express)
require('./routes/browse')(app, express)
/* -- START SERVER ----------------------------------------------
*
*
*/
portManager
.getNextAvailable(app, global.serveIp, global.servePort)
.then(port => {
global.servePort = port
app.listen(global.servePort, global.serveIp, () => {
global.origin = `http://${global.serveIp}:${global.servePort}`
global.app = `${global.origin}/app`
global.api = `${global.origin}/api`
log(`Origin: ${global.origin}`)
log(`API: ${global.api}`)
log(``)
log(`Server running at ${global.app}`)
// open browser, after server is ready
if ('browser' in argv) {
open(`${global.app}`)
}
// start request timeout, if requested
if ('timeout' in argv) {
timeout.setCsTimeout(Number(argv.timeout), cleanup)
}
})
})
.catch(err => {
log(`Unable to start server!`)
throw err
})
const background = require('./lib/compiling/background.js')
const cleanup = async () => {
await background.killAll()
log('Goodbye~')
process.exit()
}
process.stdin.resume() // so the program will not terminate instantly
process.on('SIGINT', cleanup)
process.on('SIGUSR1', cleanup)
process.on('SIGUSR2', cleanup)
process.on('SIGTERM', cleanup)
// process.on('SIGKILL', cleanup)
process.on('exit', code => {
log(`Exit code: ${code}`)
})
process.on('uncaughtException', err => {
throw err
})