-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwwwProcess.ts
78 lines (58 loc) · 1.53 KB
/
wwwProcess.ts
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
import http from 'node:http'
import Debug from 'debug'
import exitHook from 'exit-hook'
import { app } from '../app.js'
import { getConfigProperty } from '../helpers/functions.config.js'
const debug = Debug(`parking-ticket-system:wwwProcess:${process.pid}`)
interface ServerError extends Error {
syscall: string
code: string
}
function onError(error: ServerError): void {
if (error.syscall !== 'listen') {
throw error
}
let doProcessExit = false
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES': {
debug('Requires elevated privileges')
doProcessExit = true
break
}
case 'EADDRINUSE': {
debug('Port is already in use.')
doProcessExit = true
break
}
default: {
throw error
}
}
if (doProcessExit) {
// eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit
process.exit(1)
}
}
function onListening(server: http.Server): void {
const addr = server.address()
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr?.port.toString() ?? ''}`
debug(`Listening on ${bind}`)
}
/**
* Initialize HTTP
*/
process.title = `${getConfigProperty('application.applicationName')} (Worker)`
const httpPort = getConfigProperty('application.httpPort')
const httpServer = http.createServer(app)
httpServer.listen(httpPort)
httpServer.on('error', onError)
httpServer.on('listening', () => {
onListening(httpServer)
})
exitHook(() => {
debug('Closing HTTP')
httpServer.close()
})