-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
executable file
·93 lines (80 loc) · 2.63 KB
/
index.js
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
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
import express from "express";
import * as https from "https";
import * as apiLayerService from "@zowe/apiml-onboarding-enabler-nodejs";
// Command-line arguments:
const args = {
port: 10020,
serviceId: "hwexpress",
// On z/OS, you need to use certificates encoded in EBCDIC
// The APIML stores such certificates in files with `-ebcdic` suffix
};
const app = express();
let httpsServer;
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
/**
* Registers the service to the APIML Discovery service
*/
/**
* Starts the REST API service as an HTTPS server
*/
function startHttpsService() {
// Index page with a link to the REST API endpoint:
app.get("/", (req, res) =>
res.json({
links: [
{
rel: "hello",
href: `${req.protocol}://${req.get("Host")}/api/v1/hello`
}
]
})
);
// REST API endpoint:
app.get("/api/v1/hello", (req, res) => res.json({ greeting: "Hello World!" }));
// Status and health endpoints for Eureka:
app.get("/api/v1/info", (req, res) => res.json({ serviceId: args.serviceId, nodeJsVersion: process.version }));
app.get("/api/v1/status", (req, res) => res.json({ status: "UP" }));
// Static resources (contains Swagger JSON document with API documentation):
app.use(express.static("src/static"));
// Start HTTPS server and register to Discovery Service:
const tlsOptions = apiLayerService.tlsOptions;
httpsServer = https.createServer(tlsOptions, app);
httpsServer.listen(args.port, function () {
console.log(`${args.serviceId} service listening on port ${args.port}`);
apiLayerService.connectToEureka();
});
}
startHttpsService();
process.on('SIGTERM', signal => {
apiLayerService.unregisterFromEureka();
httpsServer.close(() => {
process.exit(0);
});
});
process.on('SIGINT', signal => {
apiLayerService.unregisterFromEureka();
httpsServer.close(() => {
process.exit(0);
});
});
process.on('uncaughtException', err => {
apiLayerService.unregisterFromEureka();
httpsServer.close(() => {
process.exit(1);
});
});
process.on('unhandledRejection', (reason, promise) => {
apiLayerService.unregisterFromEureka();
httpsServer.close(() => {
process.exit(1);
});
});