-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
HttpServer.js
60 lines (50 loc) · 1.42 KB
/
HttpServer.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
import { Server } from '@hapi/hapi'
import { catchAllRoute, connectionsRoutes } from './http-routes/index.js'
import serverlessLog from '../../serverlessLog.js'
export default class HttpServer {
constructor(options, webSocketClients) {
this._options = options
this._server = null
this._webSocketClients = webSocketClients
const { host, websocketPort } = options
const serverOptions = {
host,
port: websocketPort,
router: {
// allows for paths with trailing slashes to be the same as without
// e.g. : /my-path is the same as /my-path/
stripTrailingSlash: true,
},
}
this._server = new Server(serverOptions)
}
async start() {
// add routes
const routes = [...connectionsRoutes(this._webSocketClients), catchAllRoute]
this._server.route(routes)
const { host, httpsProtocol, websocketPort } = this._options
try {
await this._server.start()
} catch (err) {
console.error(
`Unexpected error while starting serverless-offline websocket server on port ${websocketPort}:`,
err,
)
process.exit(1)
}
serverlessLog(
`Offline [http for websocket] listening on http${
httpsProtocol ? 's' : ''
}://${host}:${websocketPort}`,
)
}
// stops the server
stop(timeout) {
return this._server.stop({
timeout,
})
}
get server() {
return this._server.listener
}
}