forked from heusalagroup/fi.hg.ssr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
252 lines (212 loc) · 7.86 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2021. Heusala Group Oy <[email protected]>. All rights reserved.
import { Server as HttpServer, createServer as createHttpServer, IncomingMessage, ServerResponse } from 'http';
import { ProcessUtils } from "../core/ProcessUtils";
import { RequestServerImpl } from "../node/RequestServerImpl";
// Must be first import to define environment variables before anything else
ProcessUtils.initEnvFromDefaultFiles();
import {
BACKEND_API_PROXY_URL,
BACKEND_API_URL, BACKEND_ENABLE_GZIP,
BACKEND_LOG_LEVEL,
BACKEND_PORT,
BACKEND_SCRIPT_NAME,
DISCORD_LOG_LEVEL,
DISCORD_LOG_NAME,
DISCORD_LOG_URL
} from "./constants/runtime";
import { LogService } from "../core/LogService";
LogService.setLogLevel(BACKEND_LOG_LEVEL);
import { ExitStatus } from "./types/ExitStatus";
import { LogLevel } from "../core/types/LogLevel";
import { RequestClientImpl } from "../core/RequestClientImpl";
import { RequestRouterImpl } from "../core/requestServer/RequestRouterImpl";
import { Headers } from "../core/request/types/Headers";
import { HttpServerController } from "./controller/HttpServerController";
import { HttpService } from "../core/HttpService";
import { HgNode } from "../node/HgNode";
import { isString } from "../core/types/String";
import { ConsoleLogger } from "../core/logger/console/ConsoleLogger";
import { DiscordLogger } from "../core/logger/discord/DiscordLogger";
import { CompositeLogger } from "../core/logger/composite/CompositeLogger";
const LOG = LogService.createLogger('main');
let ALL_REACT_ROUTES : readonly string[] = [];
export function setMainReactRoutes (routes : readonly string[]) {
ALL_REACT_ROUTES = routes;
}
export async function main (
args: any[] = []
) : Promise<ExitStatus> {
let server : HttpServer | undefined;
try {
Headers.setLogLevel(LogLevel.INFO);
RequestRouterImpl.setLogLevel(LogLevel.DEBUG);
RequestClientImpl.setLogLevel(LogLevel.INFO);
RequestServerImpl.setLogLevel(LogLevel.DEBUG);
// SimpleMatrixClient.setLogLevel(LogLevel.INFO);
// MatrixCrudRepository.setLogLevel(LogLevel.INFO);
HgNode.initialize();
// Setup logging
LogService.setLogLevel(LogLevel.DEBUG);
const consoleLogger = new ConsoleLogger();
consoleLogger.setLogLevel(BACKEND_LOG_LEVEL);
const discordLogger = DISCORD_LOG_URL ? new DiscordLogger(
DISCORD_LOG_NAME,
DISCORD_LOG_URL,
DISCORD_LOG_LEVEL
) : undefined;
const mainLogger = new CompositeLogger(
[
consoleLogger,
...(discordLogger ? [discordLogger]: [])
]
);
LogService.setLogger(mainLogger);
LOG.debug(`Loglevel as ${LogService.getLogLevelString()}`);
args.shift();
args.shift();
const appDir : string | undefined = args.shift();
const appComponent : string | undefined = args.shift();
const initFile : string | undefined = args.shift();
if ( !isString(appDir) || !appComponent ) {
LOG.error(`USAGE: ${BACKEND_SCRIPT_NAME} APP_DIR APP_COMPONENT_FILE`);
return ExitStatus.USAGE;
}
// Hijack require for TypeScript ES2020 interop
const ModuleM = require('module');
const Module = (ModuleM as any)?.default ?? ModuleM;
const {require: oldRequire} = Module.prototype;
Module.prototype.require = function hijacked (file: string) {
// LOG.debug(`Loading 1: "${file}"`);
// noinspection JSVoidFunctionReturnValueUsed
const result = oldRequire.apply(this, [file]);
return result?.default ?? result;
};
if (BACKEND_API_URL) {
LOG.debug(`Internal backend API set as: ${BACKEND_API_URL}`);
HttpService.setBaseUrl(BACKEND_API_URL);
} else {
LOG.warn(`Warning! No BACKEND_API_URL defined. HTTP calls may not work correctly.`);
}
if (initFile) {
require(initFile);
}
const App = isString(appComponent) ? require(appComponent) : appComponent;
const httpController = new HttpServerController(
appDir,
App,
BACKEND_API_PROXY_URL,
ALL_REACT_ROUTES,
300,
'nor-ssr-server',
BACKEND_ENABLE_GZIP
);
server = createHttpServer(
(
req : IncomingMessage,
res : ServerResponse
) => {
handleSafeServerRequest(httpController, req, res).catch((err => {
LOG.error(`Unexpected error in request handler: `, err);
}));
}
);
server.listen(BACKEND_PORT);
server.on('error', onError);
server.on('listening', onListening);
const stopPromise = new Promise<void>((resolve, reject) => {
try {
if (!server) {
reject(`No server defined`);
return;
}
server.once('close', () => {
LOG.debug('Stopping server from RequestServer stop event');
resolve();
});
} catch(err) {
reject(err);
}
});
ProcessUtils.setupDestroyHandler( () => {
if (server) {
server.removeListener('error', onError);
server.removeListener('listening', onListening);
}
LOG.debug('Stopping server from process utils event');
if (server?.close) {
server.close();
}
}, (err : any) => {
LOG.error('Error while shutting down the service: ', err);
});
await stopPromise;
return ExitStatus.OK;
} catch (err) {
LOG.error(`Fatal error: `, err);
return ExitStatus.FATAL_ERROR;
}
/**
*
* @param httpController
* @param req
* @param res
*/
async function handleSafeServerRequest (
httpController : HttpServerController,
req : IncomingMessage,
res : ServerResponse
) {
const method = req?.method;
const url = req?.url;
try {
LOG.debug(`"${method} ${url}": Request started...`);
await httpController.handleRequest(req, res);
LOG.debug(`"${method} ${url}": Request ended`);
} catch (err: any) {
LOG.error(`"${method} ${url}": Exception caught: `, err);
} finally {
if (!res.writableEnded) {
LOG.warn(`"${method} ${url}": Warning! Request handler did not close the response.`);
res.end();
}
}
}
/**
* Event listener for HTTP server "error" event.
*/
function onError (error : any) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof BACKEND_PORT === 'string'
? 'Pipe ' + BACKEND_PORT
: 'Port ' + BACKEND_PORT;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening () {
if (!server) {
LOG.info('Listening on unknown');
return;
}
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + (addr ? addr.port : 'unknown');
LOG.info('Listening on ' + bind);
}
}