Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
53 lines (43 loc) · 1.82 KB
/
Copy pathserver.ts
File metadata and controls
53 lines (43 loc) · 1.82 KB
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
import 'source-map-support/register';
import http from 'http';
import chalk from 'chalk';
import { WebSocketServer } from 'ws';
import app from './server/app';
import configs from "./server/helpers/configs";
import { onWsConnection } from "./server/routes/websockets";
if(typeof configs.isTTY === "boolean") {
chalk.level = configs.isTTY ? (chalk.level || 1) : 0;
}
let port = configs.port || 3000;
if(process.env.PORT) port = parseInt(process.env.PORT) || port;
let host = configs.host || "0.0.0.0";
if(process.env.HOST) host = process.env.HOST;
const server = http.createServer(app);
const wss = new WebSocketServer({ noServer: true });
let currentWsHandler = onWsConnection;
server.on('upgrade', (req, soc, head) => currentWsHandler(wss, req, soc, head));
server.listen({ port, host });
console.log(`\n${chalk.bold("Hybooru")} started on ${chalk.yellow.bold(`http://${host === "0.0.0.0" ? "127.0.0.1" : host}:${port}`)}`);
console.log(`Environment: ${chalk.yellow.bold("" + process.env.NODE_ENV)}.`);
console.log(chalk.dim.white(`Press Ctrl-C to terminate.\n`));
if(module.hot) {
const origApp = app;
let currentApp = app;
module.hot.accept('./server/app', () => {
let newApp = app;
if(origApp === newApp) newApp = require("./server/app").default;
server.removeListener('request', currentApp);
server.on('request', newApp);
currentApp = newApp;
});
const origWsHandler = onWsConnection;
module.hot.accept('./server/routes/websockets', () => {
let newWsHandler = onWsConnection;
if(origWsHandler === newWsHandler) newWsHandler = require("./server/routes/websockets").onWsConnection;
wss.clients.forEach(client => client.close());
currentWsHandler = newWsHandler;
});
}
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});