-
Notifications
You must be signed in to change notification settings - Fork 0
/
before_serve.js
166 lines (159 loc) · 4.89 KB
/
before_serve.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
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
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
"use strict";
/**
* Uses environment variables specified at build time to generate an env.js file in the Javascript output directory
* The env.js file is sourced by index.html to make those values available to the application at runtime.
*/
function createEnvJs() {
const fs = require("fs");
const envJsFilePath = "web/js/env.js";
try {
fs.unlinkSync(envJsFilePath);
console.log(`Removed existing environment file ${envJsFilePath}`);
} catch (e) {
if (e.message.includes("ENOENT")) {
console.log(`No existing ${envJsFilePath} found`);
} else {
console.log(`Error deleting existing ${envJsFilePath}: ${e}`);
throw e;
}
}
try {
console.log("Creating env.js.");
fs.writeFileSync(
`${envJsFilePath}`,
`var vzApiUrl = "${
process.env.VZ_API_URL || ""
}"; var vzWLSImagesEnabled = ${
process.env.VZ_WLS_IMAGES_ENABLED || false
};`,
{ flag: "wx" }
);
console.log(`${envJsFilePath} created.`);
} catch (e) {
console.log(`Failed creating ${envJsFilePath}: ${e}`);
throw e;
}
}
function rewriteUrls() {
const express = require("express");
const app = express();
app.get("/oamapps", (req, res, next) => {
res.redirect(
`/?ojr=instance&selectedItem=oamapps${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/oamcomps", (req, res, next) => {
res.redirect(
`/?ojr=instance&selectedItem=oamcomps${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/projects", (req, res, next) => {
res.redirect(`/?ojr=instance&selectedItem=projects`);
});
app.get("/weblogicimages", (req, res, next) => {
res.redirect(
`/?ojr=instance&selectedItem=weblogicimages${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/oamapps/:id", (req, res, next) => {
res.redirect(
`/?ojr=oamapp&oamAppId=${req.params.id}${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/oamcomps/:id", (req, res, next) => {
res.redirect(
`/?ojr=oamcomp&oamCompId=${req.params.id}${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/projects/:id", (req, res, next) => {
res.redirect(`/?ojr=project&projectId=${req.params.id}`);
});
app.get("/oamapps/:id/components", (req, res, next) => {
res.redirect(
`/?ojr=oamapp&oamAppId=${req.params.id}&selectedItem=components${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
});
app.get("/oamapps/:id/components/:selectedComponent", (req, res, next) => {
res.redirect(
`/?ojr=oamapp&oamAppId=${
req.params.id
}&selectedItem=components&selectedComponent=${
req.params.selectedComponent
}${req.query.cluster ? "&cluster=" + req.query.cluster : ""}`
);
});
app.get(
"/oamapps/:id/components/:selectedComponent/:selectedItem",
(req, res, next) => {
res.redirect(
`/?ojr=oamapp&oamAppId=${req.params.id}&selectedItem=${
req.params.selectedItem
}&selectedComponent=${req.params.selectedComponent}${
req.query.cluster ? "&cluster=" + req.query.cluster : ""
}`
);
}
);
app.get("/oamcomps/:id/:selectedItem", (req, res, next) => {
res.redirect(
`/?ojr=oamcomp&oamCompId=${req.params.id}&selectedItem=${
req.params.selectedItem
}${req.query.cluster ? "&cluster=" + req.query.cluster : ""}`
);
});
app.get("/clusters", (req, res, next) => {
res.redirect("/?ojr=instance&selectedItem=clusters");
});
app.get("/projects/:id/namespaces", (req, res, next) => {
res.redirect(
`/?ojr=project&projectId=${req.params.id}&selectedItem=namespaces`
);
});
app.get("/projects/:id/clusters", (req, res, next) => {
res.redirect(
`/?ojr=project&projectId=${req.params.id}&selectedItem=clusters`
);
});
app.get("/projects/:id/security", (req, res, next) => {
res.redirect(
`/?ojr=project&projectId=${req.params.id}&selectedItem=security`
);
});
app.get("/projects/:id/networkPolicies", (req, res, next) => {
res.redirect(
`/?ojr=project&projectId=${req.params.id}&selectedItem=networkPolicies`
);
});
app.get("/", (req, res, next) => {
if (redirectOnce && process.env.VZ_API_URL) {
redirectOnce = false;
req.res.redirect(process.env.VZ_API_URL);
} else {
next();
}
});
return app;
}
var redirectOnce = true;
module.exports = function (configObj) {
return new Promise((resolve, reject) => {
console.log("Running before_serve hook.");
createEnvJs();
configObj.express = rewriteUrls();
resolve(configObj);
});
};