2020-10-16 19:43:49 +02:00
|
|
|
import { logger } from "@coder/logger"
|
|
|
|
import express, { Express } from "express"
|
|
|
|
import { promises as fs } from "fs"
|
|
|
|
import http from "http"
|
|
|
|
import * as httpolyglot from "httpolyglot"
|
|
|
|
import { DefaultedArgs } from "./cli"
|
2020-11-05 19:58:37 +01:00
|
|
|
import { handleUpgrade } from "./wsRouter"
|
2020-10-16 19:43:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an Express app and an HTTP/S server to serve it.
|
|
|
|
*/
|
2020-11-05 19:58:37 +01:00
|
|
|
export const createApp = async (args: DefaultedArgs): Promise<[Express, Express, http.Server]> => {
|
2020-10-16 19:43:49 +02:00
|
|
|
const app = express()
|
|
|
|
|
|
|
|
const server = args.cert
|
|
|
|
? httpolyglot.createServer(
|
|
|
|
{
|
|
|
|
cert: args.cert && (await fs.readFile(args.cert.value)),
|
|
|
|
key: args["cert-key"] && (await fs.readFile(args["cert-key"])),
|
|
|
|
},
|
|
|
|
app,
|
|
|
|
)
|
|
|
|
: http.createServer(app)
|
|
|
|
|
|
|
|
await new Promise<http.Server>(async (resolve, reject) => {
|
|
|
|
server.on("error", reject)
|
|
|
|
if (args.socket) {
|
|
|
|
try {
|
|
|
|
await fs.unlink(args.socket)
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== "ENOENT") {
|
|
|
|
logger.error(error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server.listen(args.socket, resolve)
|
|
|
|
} else {
|
|
|
|
// [] is the correct format when using :: but Node errors with them.
|
|
|
|
server.listen(args.port, args.host.replace(/^\[|\]$/g, ""), resolve)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-05 19:58:37 +01:00
|
|
|
const wsApp = express()
|
|
|
|
handleUpgrade(wsApp, server)
|
2020-10-21 01:05:58 +02:00
|
|
|
|
2020-11-05 19:58:37 +01:00
|
|
|
return [app, wsApp, server]
|
2020-10-16 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-03 23:11:28 +01:00
|
|
|
* Get the address of a server as a string (protocol *is* included) while
|
2020-10-16 19:43:49 +02:00
|
|
|
* ensuring there is one (will throw if there isn't).
|
|
|
|
*/
|
|
|
|
export const ensureAddress = (server: http.Server): string => {
|
|
|
|
const addr = server.address()
|
|
|
|
if (!addr) {
|
|
|
|
throw new Error("server has no address")
|
|
|
|
}
|
|
|
|
if (typeof addr !== "string") {
|
2020-11-03 23:11:28 +01:00
|
|
|
return `http://${addr.address}:${addr.port}`
|
2020-10-16 19:43:49 +02:00
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|