This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
forked from huggingface/Mongoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
140 lines (119 loc) · 3.38 KB
/
server.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
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
140
import * as path from "path";
import * as fs from "fs";
import http from "http";
import express from "express";
const app = express();
import factory from "./lib/Factory";
import { api } from "./routes/api";
const setupServer = () => {
const SERVER_PORT = process.env.MONGOKU_SERVER_PORT || 3100;
app.get("/", (req, res, next) => {
res.sendFile("app/index.html", { root: __dirname }, (err) => {
if (err) {
return next(err);
}
});
});
app.use("/api", api);
app.get("/*", (req, res, next) => {
const ext = path.extname(req.url);
fs.stat(path.join(__dirname, "app", req.url), (err, stats) => {
let file = "index.html";
if (stats && stats.isFile()) {
file = req.url;
}
res.sendFile(file, { root: path.join(__dirname, "app") }, (err) => {
if (err) {
return next(err);
}
});
});
});
app.use((err: Error, req: express.Request, res: express.Response, next) => {
res.status(500);
return res.json({
ok: false,
message: err.message,
});
});
server.listen(SERVER_PORT, () =>
console.log(`[Mongoku] listening on port ` + SERVER_PORT)
);
};
const server = http.createServer(app);
export const start = async () => {
console.log(`[Mongoku] Starting...`);
try {
await factory.load();
setupServer();
} catch (err) {
console.error(err);
process.exit(1);
}
};
if (require.main === module) {
(async () => {
await start();
})();
}
// BretFisher / node-docker-good-defaults
// see https://github.com/BretFisher/node-docker-good-defaults/blob/master/bin/www
// need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
// this also won't work on using npm start since:
// https://github.com/npm/npm/issues/4603
// https://github.com/npm/npm/pull/10868
// https://github.com/RisingStack/kubernetes-graceful-shutdown-example/blob/master/src/index.js
// if you want to use npm then start with `docker run --init` to help, but I still don't think it's
// a graceful shutdown of node process
//
// TODO: Not strongly types
const sockets: { [key: string]: any } = {};
let nextSocketId = 0;
function waitForSocketsToClose(counter: number): any {
if (counter > 0) {
console.log(
`Waiting ${counter} more ${
counter === 1 ? "seconds" : "second"
} for all connections to close...`
);
return setTimeout(waitForSocketsToClose, 1000, counter - 1);
}
console.log("Forcing all connections to close now");
for (const socketId in sockets) {
sockets[socketId].destroy();
}
}
server.on("connection", function (socket) {
const socketId = nextSocketId++;
sockets[socketId] = socket;
socket.once("close", function () {
delete sockets[socketId];
});
});
// shut down server
function shutdown(): void {
waitForSocketsToClose(10);
server.close(function onServerClosed(err) {
if (err) {
console.error(err);
process.exitCode = 1;
}
process.exit();
});
}
// quit on ctrl-c when running docker in terminal
process.on("SIGINT", function onSigint() {
console.info(
"Got SIGINT (aka ctrl-c in docker). Graceful shutdown ",
new Date().toISOString()
);
shutdown();
});
// quit properly on docker stop
process.on("SIGTERM", function onSigterm() {
console.info(
"Got SIGTERM (docker container stop). Graceful shutdown ",
new Date().toISOString()
);
shutdown();
});