Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/servers/http.ts
1503 views
1
// The HTTP(S) server, which makes the other servers
2
// (websocket, proxy, and share) available on the network.
3
4
import { Application } from "express";
5
import { readFileSync } from "fs";
6
import { getLogger } from "../logger";
7
import { createServer as httpsCreateServer } from "https";
8
import { createServer as httpCreateServer } from "http";
9
10
interface Options {
11
cert?: string;
12
key?: string;
13
app: Application;
14
}
15
16
const logger = getLogger("http:server");
17
export default function init({ cert, key, app }: Options) {
18
let httpServer;
19
if (key || cert) {
20
if (!key || !cert) {
21
throw Error("specify *both* key and cert or neither");
22
}
23
logger.info("Creating HTTPS server...");
24
httpServer = httpsCreateServer(
25
{
26
cert: readFileSync(cert),
27
key: readFileSync(key),
28
},
29
app,
30
);
31
} else {
32
logger.info("Creating HTTP server...");
33
httpServer = httpCreateServer(app);
34
}
35
httpServer.on("error", (err) => {
36
logger.error(`WARNING -- hub http server error: ${err.stack || err}`);
37
});
38
39
return httpServer;
40
}
41
42