@centeredgesoft/runtime-mock-routes 2.10.0 → 2.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/run.js +2 -98
- package/build/server.d.ts +7 -0
- package/build/server.js +104 -0
- package/package.json +1 -1
- package/run.ts +2 -97
- package/server.ts +103 -0
package/build/run.js
CHANGED
|
@@ -1,41 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
const fs_1 = __importDefault(require("fs"));
|
|
17
|
-
const http_1 = __importDefault(require("http"));
|
|
18
|
-
const https_1 = __importDefault(require("https"));
|
|
19
|
-
const commander_1 = require("commander");
|
|
20
|
-
const path_1 = require("path");
|
|
21
|
-
const app_1 = require("./src/app");
|
|
22
|
-
const appV2_1 = require("./src/appV2");
|
|
23
4
|
const sdk_node_1 = require("@opentelemetry/sdk-node");
|
|
24
5
|
const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
|
|
25
6
|
const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
|
|
26
|
-
var AppVersion;
|
|
27
|
-
(function (AppVersion) {
|
|
28
|
-
AppVersion["v1"] = "v1";
|
|
29
|
-
AppVersion["v2"] = "v2";
|
|
30
|
-
})(AppVersion || (AppVersion = {}));
|
|
31
|
-
commander_1.program.version("2.1.0")
|
|
32
|
-
.option("-p, --port <port>", "Port to run the server one", process.env.RUNTIME_MOCK_ROUTES_PORT || "8080")
|
|
33
|
-
.option("--https-port <httpsPort>", "Enable TLS for the server", process.env.RUNTIME_MOCK_ROUTES_HTTPS_PORT || "8443")
|
|
34
|
-
.option("--tlsCert <tlsCertPath>", "Path to the TLS certificate file", process.env.HTTPS_CERT_FILE)
|
|
35
|
-
.option("--tlsKey <tlsKeyPath>", "Path to the TLS key file", process.env.HTTPS_CERT_KEY_FILE)
|
|
36
|
-
.option("-s, --seed <filePath>", "File path to seed the application", process.env.RUNTIME_MOCK_ROUTES_FILE_PATH)
|
|
37
|
-
.option("-a, --appVersion <appVersion>", "App Version to use", AppVersion.v2);
|
|
38
|
-
commander_1.program.parse(process.argv);
|
|
39
7
|
let logRecordProcessors = undefined;
|
|
40
8
|
if (!process.env.OTEL_EXPORTER_OTLP_ENDPOINT) {
|
|
41
9
|
// OTLP isn't configured, fallback to console logging
|
|
@@ -49,69 +17,5 @@ const sdk = new sdk_node_1.NodeSDK({
|
|
|
49
17
|
],
|
|
50
18
|
});
|
|
51
19
|
sdk.start();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const filePath = (0, path_1.resolve)(commander_1.program.seed);
|
|
55
|
-
const funcOrJson = require(filePath);
|
|
56
|
-
initialRequests = typeof funcOrJson === 'function' ? funcOrJson() : funcOrJson;
|
|
57
|
-
}
|
|
58
|
-
const appVersions = {
|
|
59
|
-
v1: app_1.appFactory,
|
|
60
|
-
v2: appV2_1.appFactory
|
|
61
|
-
};
|
|
62
|
-
const appFactory = appVersions[commander_1.program.appVersion] || appV2_1.appFactory;
|
|
63
|
-
const app = appFactory(initialRequests);
|
|
64
|
-
// Setup HTTPS options
|
|
65
|
-
const httpsOptions = commander_1.program.tlsCert && commander_1.program.tlsKey && fs_1.default.existsSync(commander_1.program.tlsCert) && fs_1.default.existsSync(commander_1.program.tlsKey)
|
|
66
|
-
? {
|
|
67
|
-
cert: fs_1.default.readFileSync(commander_1.program.tlsCert),
|
|
68
|
-
key: fs_1.default.readFileSync(commander_1.program.tlsKey),
|
|
69
|
-
enabled: true
|
|
70
|
-
}
|
|
71
|
-
: { enabled: false };
|
|
72
|
-
const httpServer = http_1.default.createServer(app);
|
|
73
|
-
const httpsServer = httpsOptions.enabled ? https_1.default.createServer(httpsOptions, app) : null;
|
|
74
|
-
httpServer.listen(commander_1.program.port, () => {
|
|
75
|
-
console.log(`⚡️[server]: Server is running at http://localhost:${commander_1.program.port}`);
|
|
76
|
-
});
|
|
77
|
-
if (httpsServer) {
|
|
78
|
-
httpsServer.listen(commander_1.program.httpsPort, () => {
|
|
79
|
-
console.log(`⚡️[server]: Server is running at https://localhost:${commander_1.program.httpsPort}`);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
// Register signal handlers
|
|
83
|
-
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
84
|
-
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
85
|
-
// Graceful shutdown handler
|
|
86
|
-
let isShuttingDown = false;
|
|
87
|
-
function gracefulShutdown(signal) {
|
|
88
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
-
if (isShuttingDown)
|
|
90
|
-
return;
|
|
91
|
-
isShuttingDown = true;
|
|
92
|
-
// Close servers
|
|
93
|
-
const closePromises = [];
|
|
94
|
-
closePromises.push(closeServer(httpServer));
|
|
95
|
-
closePromises.push(closeServer(httpsServer));
|
|
96
|
-
yield Promise.all(closePromises);
|
|
97
|
-
try {
|
|
98
|
-
yield sdk.shutdown();
|
|
99
|
-
}
|
|
100
|
-
catch (err) {
|
|
101
|
-
console.log("Error shutting down OTEL SDK", err);
|
|
102
|
-
}
|
|
103
|
-
console.log('Shutdown complete, exiting process');
|
|
104
|
-
process.exit(0);
|
|
105
|
-
function closeServer(httpServer) {
|
|
106
|
-
if (!httpServer)
|
|
107
|
-
return Promise.resolve();
|
|
108
|
-
const serverType = httpServer instanceof https_1.default.Server ? 'HTTPS' : 'HTTP';
|
|
109
|
-
return new Promise(resolve => {
|
|
110
|
-
httpServer.close(() => {
|
|
111
|
-
resolve();
|
|
112
|
-
});
|
|
113
|
-
httpServer.closeAllConnections();
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
20
|
+
// Delay import so we register the OTEL SDK before loading modules like http/express
|
|
21
|
+
require("./server").runServer(sdk);
|
package/build/server.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.runServer = exports.AppVersion = void 0;
|
|
16
|
+
const fs_1 = __importDefault(require("fs"));
|
|
17
|
+
const http_1 = __importDefault(require("http"));
|
|
18
|
+
const https_1 = __importDefault(require("https"));
|
|
19
|
+
const commander_1 = require("commander");
|
|
20
|
+
const path_1 = require("path");
|
|
21
|
+
const app_1 = require("./src/app");
|
|
22
|
+
const appV2_1 = require("./src/appV2");
|
|
23
|
+
var AppVersion;
|
|
24
|
+
(function (AppVersion) {
|
|
25
|
+
AppVersion["v1"] = "v1";
|
|
26
|
+
AppVersion["v2"] = "v2";
|
|
27
|
+
})(AppVersion || (exports.AppVersion = AppVersion = {}));
|
|
28
|
+
const runServer = (sdk) => {
|
|
29
|
+
commander_1.program.version("2.1.0")
|
|
30
|
+
.option("-p, --port <port>", "Port to run the server one", process.env.RUNTIME_MOCK_ROUTES_PORT || "8080")
|
|
31
|
+
.option("--https-port <httpsPort>", "Enable TLS for the server", process.env.RUNTIME_MOCK_ROUTES_HTTPS_PORT || "8443")
|
|
32
|
+
.option("--tlsCert <tlsCertPath>", "Path to the TLS certificate file", process.env.HTTPS_CERT_FILE)
|
|
33
|
+
.option("--tlsKey <tlsKeyPath>", "Path to the TLS key file", process.env.HTTPS_CERT_KEY_FILE)
|
|
34
|
+
.option("-s, --seed <filePath>", "File path to seed the application", process.env.RUNTIME_MOCK_ROUTES_FILE_PATH)
|
|
35
|
+
.option("-a, --appVersion <appVersion>", "App Version to use", AppVersion.v2);
|
|
36
|
+
commander_1.program.parse(process.argv);
|
|
37
|
+
let initialRequests = {};
|
|
38
|
+
if (commander_1.program.seed) {
|
|
39
|
+
const filePath = (0, path_1.resolve)(commander_1.program.seed);
|
|
40
|
+
const funcOrJson = require(filePath);
|
|
41
|
+
initialRequests = typeof funcOrJson === 'function' ? funcOrJson() : funcOrJson;
|
|
42
|
+
}
|
|
43
|
+
const appVersions = {
|
|
44
|
+
v1: app_1.appFactory,
|
|
45
|
+
v2: appV2_1.appFactory
|
|
46
|
+
};
|
|
47
|
+
const appFactory = appVersions[commander_1.program.appVersion] || appV2_1.appFactory;
|
|
48
|
+
const app = appFactory(initialRequests);
|
|
49
|
+
// Setup HTTPS options
|
|
50
|
+
const httpsOptions = commander_1.program.tlsCert && commander_1.program.tlsKey && fs_1.default.existsSync(commander_1.program.tlsCert) && fs_1.default.existsSync(commander_1.program.tlsKey)
|
|
51
|
+
? {
|
|
52
|
+
cert: fs_1.default.readFileSync(commander_1.program.tlsCert),
|
|
53
|
+
key: fs_1.default.readFileSync(commander_1.program.tlsKey),
|
|
54
|
+
enabled: true
|
|
55
|
+
}
|
|
56
|
+
: { enabled: false };
|
|
57
|
+
const httpServer = http_1.default.createServer(app);
|
|
58
|
+
const httpsServer = httpsOptions.enabled ? https_1.default.createServer(httpsOptions, app) : null;
|
|
59
|
+
httpServer.listen(commander_1.program.port, () => {
|
|
60
|
+
console.log(`⚡️[server]: Server is running at http://localhost:${commander_1.program.port}`);
|
|
61
|
+
});
|
|
62
|
+
if (httpsServer) {
|
|
63
|
+
httpsServer.listen(commander_1.program.httpsPort, () => {
|
|
64
|
+
console.log(`⚡️[server]: Server is running at https://localhost:${commander_1.program.httpsPort}`);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Register signal handlers
|
|
68
|
+
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
69
|
+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
70
|
+
// Graceful shutdown handler
|
|
71
|
+
let isShuttingDown = false;
|
|
72
|
+
function gracefulShutdown(signal) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
if (isShuttingDown)
|
|
75
|
+
return;
|
|
76
|
+
isShuttingDown = true;
|
|
77
|
+
// Close servers
|
|
78
|
+
const closePromises = [];
|
|
79
|
+
closePromises.push(closeServer(httpServer));
|
|
80
|
+
closePromises.push(closeServer(httpsServer));
|
|
81
|
+
yield Promise.all(closePromises);
|
|
82
|
+
try {
|
|
83
|
+
yield sdk.shutdown();
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
console.log("Error shutting down OTEL SDK", err);
|
|
87
|
+
}
|
|
88
|
+
console.log('Shutdown complete, exiting process');
|
|
89
|
+
process.exit(0);
|
|
90
|
+
function closeServer(httpServer) {
|
|
91
|
+
if (!httpServer)
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
const serverType = httpServer instanceof https_1.default.Server ? 'HTTPS' : 'HTTP';
|
|
94
|
+
return new Promise(resolve => {
|
|
95
|
+
httpServer.close(() => {
|
|
96
|
+
resolve();
|
|
97
|
+
});
|
|
98
|
+
httpServer.closeAllConnections();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
exports.runServer = runServer;
|
package/package.json
CHANGED
package/run.ts
CHANGED
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import http from 'http';
|
|
4
|
-
import https from 'https';
|
|
5
|
-
import { program } from 'commander';
|
|
6
|
-
import { resolve } from 'path';
|
|
7
|
-
import { appFactory as appFactoryV1 } from './src/app';
|
|
8
|
-
import { appFactory as appFactoryV2 } from './src/appV2';
|
|
9
|
-
import { Express } from 'express'
|
|
10
2
|
import { NodeSDK, logs } from '@opentelemetry/sdk-node';
|
|
11
3
|
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
|
|
12
4
|
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
|
|
13
5
|
|
|
14
|
-
enum AppVersion {
|
|
15
|
-
v1 = 'v1',
|
|
16
|
-
v2 = 'v2'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
program.version("2.1.0")
|
|
20
|
-
.option("-p, --port <port>", "Port to run the server one", process.env.RUNTIME_MOCK_ROUTES_PORT || "8080")
|
|
21
|
-
.option("--https-port <httpsPort>", "Enable TLS for the server", process.env.RUNTIME_MOCK_ROUTES_HTTPS_PORT || "8443")
|
|
22
|
-
.option("--tlsCert <tlsCertPath>", "Path to the TLS certificate file", process.env.HTTPS_CERT_FILE)
|
|
23
|
-
.option("--tlsKey <tlsKeyPath>", "Path to the TLS key file", process.env.HTTPS_CERT_KEY_FILE)
|
|
24
|
-
.option("-s, --seed <filePath>", "File path to seed the application", process.env.RUNTIME_MOCK_ROUTES_FILE_PATH)
|
|
25
|
-
.option("-a, --appVersion <appVersion>", "App Version to use", AppVersion.v2)
|
|
26
|
-
|
|
27
|
-
program.parse(process.argv);
|
|
28
|
-
|
|
29
6
|
let logRecordProcessors: logs.LogRecordProcessor[] | undefined = undefined;
|
|
30
7
|
if (!process.env.OTEL_EXPORTER_OTLP_ENDPOINT) {
|
|
31
8
|
// OTLP isn't configured, fallback to console logging
|
|
@@ -42,77 +19,5 @@ const sdk = new NodeSDK({
|
|
|
42
19
|
|
|
43
20
|
sdk.start()
|
|
44
21
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const filePath = resolve(program.seed);
|
|
48
|
-
const funcOrJson = require(filePath);
|
|
49
|
-
initialRequests = typeof funcOrJson === 'function' ? funcOrJson() : funcOrJson;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
type AppFactory = (...args: any[]) => Express;
|
|
53
|
-
|
|
54
|
-
const appVersions: Record<AppVersion, AppFactory> = {
|
|
55
|
-
v1: appFactoryV1,
|
|
56
|
-
v2: appFactoryV2
|
|
57
|
-
}
|
|
58
|
-
const appFactory = appVersions[program.appVersion] || appFactoryV2;
|
|
59
|
-
|
|
60
|
-
const app = appFactory(initialRequests);
|
|
61
|
-
|
|
62
|
-
// Setup HTTPS options
|
|
63
|
-
const httpsOptions = program.tlsCert && program.tlsKey && fs.existsSync(program.tlsCert) && fs.existsSync(program.tlsKey)
|
|
64
|
-
? {
|
|
65
|
-
cert: fs.readFileSync(program.tlsCert),
|
|
66
|
-
key: fs.readFileSync(program.tlsKey),
|
|
67
|
-
enabled: true
|
|
68
|
-
}
|
|
69
|
-
: { enabled: false };
|
|
70
|
-
|
|
71
|
-
const httpServer = http.createServer(app);
|
|
72
|
-
const httpsServer = httpsOptions.enabled ? https.createServer(httpsOptions, app) : null;
|
|
73
|
-
|
|
74
|
-
httpServer.listen(program.port, () => {
|
|
75
|
-
console.log(`⚡️[server]: Server is running at http://localhost:${program.port}`);
|
|
76
|
-
});
|
|
77
|
-
if (httpsServer) {
|
|
78
|
-
httpsServer.listen(program.httpsPort, () => {
|
|
79
|
-
console.log(`⚡️[server]: Server is running at https://localhost:${program.httpsPort}`);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Register signal handlers
|
|
84
|
-
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
85
|
-
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
86
|
-
|
|
87
|
-
// Graceful shutdown handler
|
|
88
|
-
let isShuttingDown = false;
|
|
89
|
-
async function gracefulShutdown(signal) {
|
|
90
|
-
if (isShuttingDown) return;
|
|
91
|
-
isShuttingDown = true;
|
|
92
|
-
|
|
93
|
-
// Close servers
|
|
94
|
-
const closePromises = [];
|
|
95
|
-
closePromises.push(closeServer(httpServer));
|
|
96
|
-
closePromises.push(closeServer(httpsServer));
|
|
97
|
-
await Promise.all(closePromises);
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
await sdk.shutdown();
|
|
101
|
-
} catch (err) {
|
|
102
|
-
console.log("Error shutting down OTEL SDK", err);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
console.log('Shutdown complete, exiting process');
|
|
106
|
-
process.exit(0);
|
|
107
|
-
|
|
108
|
-
function closeServer(httpServer) {
|
|
109
|
-
if (!httpServer) return Promise.resolve();
|
|
110
|
-
const serverType = httpServer instanceof https.Server ? 'HTTPS' : 'HTTP'
|
|
111
|
-
return new Promise<void>(resolve => {
|
|
112
|
-
httpServer.close(() => {
|
|
113
|
-
resolve();
|
|
114
|
-
});
|
|
115
|
-
httpServer.closeAllConnections();
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
22
|
+
// Delay import so we register the OTEL SDK before loading modules like http/express
|
|
23
|
+
require("./server").runServer(sdk);
|
package/server.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import { Express } from 'express'
|
|
5
|
+
import { program } from 'commander';
|
|
6
|
+
import { resolve } from 'path';
|
|
7
|
+
import { appFactory as appFactoryV1 } from './src/app';
|
|
8
|
+
import { appFactory as appFactoryV2 } from './src/appV2';
|
|
9
|
+
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
10
|
+
|
|
11
|
+
export enum AppVersion {
|
|
12
|
+
v1 = 'v1',
|
|
13
|
+
v2 = 'v2'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const runServer = (sdk: NodeSDK) => {
|
|
17
|
+
program.version("2.1.0")
|
|
18
|
+
.option("-p, --port <port>", "Port to run the server one", process.env.RUNTIME_MOCK_ROUTES_PORT || "8080")
|
|
19
|
+
.option("--https-port <httpsPort>", "Enable TLS for the server", process.env.RUNTIME_MOCK_ROUTES_HTTPS_PORT || "8443")
|
|
20
|
+
.option("--tlsCert <tlsCertPath>", "Path to the TLS certificate file", process.env.HTTPS_CERT_FILE)
|
|
21
|
+
.option("--tlsKey <tlsKeyPath>", "Path to the TLS key file", process.env.HTTPS_CERT_KEY_FILE)
|
|
22
|
+
.option("-s, --seed <filePath>", "File path to seed the application", process.env.RUNTIME_MOCK_ROUTES_FILE_PATH)
|
|
23
|
+
.option("-a, --appVersion <appVersion>", "App Version to use", AppVersion.v2)
|
|
24
|
+
|
|
25
|
+
program.parse(process.argv);
|
|
26
|
+
|
|
27
|
+
let initialRequests: any = {};
|
|
28
|
+
if (program.seed) {
|
|
29
|
+
const filePath = resolve(program.seed);
|
|
30
|
+
const funcOrJson = require(filePath);
|
|
31
|
+
initialRequests = typeof funcOrJson === 'function' ? funcOrJson() : funcOrJson;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type AppFactory = (...args: any[]) => Express;
|
|
35
|
+
|
|
36
|
+
const appVersions: Record<AppVersion, AppFactory> = {
|
|
37
|
+
v1: appFactoryV1,
|
|
38
|
+
v2: appFactoryV2
|
|
39
|
+
}
|
|
40
|
+
const appFactory = appVersions[program.appVersion] || appFactoryV2;
|
|
41
|
+
|
|
42
|
+
const app = appFactory(initialRequests);
|
|
43
|
+
|
|
44
|
+
// Setup HTTPS options
|
|
45
|
+
const httpsOptions = program.tlsCert && program.tlsKey && fs.existsSync(program.tlsCert) && fs.existsSync(program.tlsKey)
|
|
46
|
+
? {
|
|
47
|
+
cert: fs.readFileSync(program.tlsCert),
|
|
48
|
+
key: fs.readFileSync(program.tlsKey),
|
|
49
|
+
enabled: true
|
|
50
|
+
}
|
|
51
|
+
: { enabled: false };
|
|
52
|
+
|
|
53
|
+
const httpServer = http.createServer(app);
|
|
54
|
+
const httpsServer = httpsOptions.enabled ? https.createServer(httpsOptions, app) : null;
|
|
55
|
+
|
|
56
|
+
httpServer.listen(program.port, () => {
|
|
57
|
+
console.log(`⚡️[server]: Server is running at http://localhost:${program.port}`);
|
|
58
|
+
});
|
|
59
|
+
if (httpsServer) {
|
|
60
|
+
httpsServer.listen(program.httpsPort, () => {
|
|
61
|
+
console.log(`⚡️[server]: Server is running at https://localhost:${program.httpsPort}`);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Register signal handlers
|
|
66
|
+
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
67
|
+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
68
|
+
|
|
69
|
+
// Graceful shutdown handler
|
|
70
|
+
let isShuttingDown = false;
|
|
71
|
+
async function gracefulShutdown(signal) {
|
|
72
|
+
if (isShuttingDown) return;
|
|
73
|
+
isShuttingDown = true;
|
|
74
|
+
|
|
75
|
+
// Close servers
|
|
76
|
+
const closePromises = [];
|
|
77
|
+
closePromises.push(closeServer(httpServer));
|
|
78
|
+
closePromises.push(closeServer(httpsServer));
|
|
79
|
+
await Promise.all(closePromises);
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
await sdk.shutdown();
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.log("Error shutting down OTEL SDK", err);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
console.log('Shutdown complete, exiting process');
|
|
88
|
+
process.exit(0);
|
|
89
|
+
|
|
90
|
+
function closeServer(httpServer) {
|
|
91
|
+
if (!httpServer) return Promise.resolve();
|
|
92
|
+
const serverType = httpServer instanceof https.Server ? 'HTTPS' : 'HTTP'
|
|
93
|
+
return new Promise<void>(resolve => {
|
|
94
|
+
httpServer.close(() => {
|
|
95
|
+
resolve();
|
|
96
|
+
});
|
|
97
|
+
httpServer.closeAllConnections();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { runServer };
|