Skip to content

Commit

Permalink
refactor: simplify configuration possibilities, remove unused options
Browse files Browse the repository at this point in the history
  • Loading branch information
cramakri authored and ciyer committed Sep 16, 2022
1 parent 577e8a8 commit 72d01f3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
2 changes: 0 additions & 2 deletions helm-chart/renku-ui/templates/ui-server-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ spec:
value: {{ .Values.server.url | default (printf "%s://%s" (include "ui.protocol" .) .Values.global.renku.domain) | quote }}
- name: UI_SERVER_VERSION
value: {{ .Chart.Version | quote }}
- name: SERVER_PORT
value: {{ .Values.server.port | default (printf "8080") | quote }}
- name: GATEWAY_URL
value: {{ .Values.gateway.url | default (printf "%s://%s/api" (include "ui.protocol" .) .Values.global.renku.domain) | quote }}
- name: GATEWAY_LOGIN_PATH
Expand Down
1 change: 0 additions & 1 deletion helm-chart/renku-ui/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ client:
server:
## The URL for the server service; the URL used by the client is the server.url + /ui-server endpoint.
url:
port: 8080
replicaCount: 1

image:
Expand Down
22 changes: 13 additions & 9 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@
* limitations under the License.
*/

import { convertType } from "./utils";
import { convertType, urlJoin } from "./utils";

const SERVER = {
url: process.env.SERVER_URL,
port: convertType(process.env.SERVER_PORT) || 8080,
port: 8080,
prefix: "/ui-server",
logLevel: process.env.SERVER_LOG_LEVEL || "info",
serverUiVersion: process.env.UI_SERVER_VERSION || "unknown",
proxyTimeout: 600 * 1000, // in milliseconds
wsSuffix: "/ws",
};

const gatewayUrl = process.env.GATEWAY_URL || urlJoin(SERVER.url ?? "", "/api");

const DEPLOYMENT = {
gatewayUrl: process.env.GATEWAY_URL || SERVER.url + "/api",
gatewayLoginUrl:
(process.env.GATEWAY_URL || SERVER.url + "/api") +
(process.env.GATEWAY_LOGIN_PATH || "/auth/login"),
gatewayLogoutUrl:
(process.env.GATEWAY_URL || SERVER.url + "/api") +
(process.env.GATEWAY_LOGOUT_PATH || "/auth/logout"),
gatewayUrl,
gatewayLoginUrl: urlJoin(
gatewayUrl,
process.env.GATEWAY_LOGIN_PATH || "/auth/login"
),
gatewayLogoutUrl: urlJoin(
gatewayUrl,
process.env.GATEWAY_LOGOUT_PATH || "/auth/logout"
),
};

const SENTRY = {
Expand Down
4 changes: 3 additions & 1 deletion server/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
*/

import urlJoin from "./url-join";


/**
* Convert string to booloan or numbers. Useful to handle values coming from environmental variables.
Expand Down Expand Up @@ -134,4 +136,4 @@ function simpleHash(str: string, seed = 0): number {
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
}

export { clamp, convertType, getCookieValueByName, getRelease, simpleHash, sleep };
export { clamp, convertType, getCookieValueByName, getRelease, simpleHash, sleep, urlJoin };
70 changes: 70 additions & 0 deletions server/src/utils/url-join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable brace-style */
/* eslint-disable no-useless-escape */
/* eslint-disable curly */

/** url-join library from https://github.com/jfromaniello/url-join/blob/main/lib/url-join.js */
function normalize(strArray: string[]) {
const resultArray = [];
if (strArray.length === 0) {
return "";
}

if (typeof strArray[0] !== "string") {
throw new TypeError("Url must be a string. Received " + strArray[0]);
}

// If the first part is a plain protocol, we combine it with the next part.
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
strArray[0] = strArray.shift() + strArray[0];
}

// There must be two or three slashes in the file protocol, two slashes in anything else.
if (strArray[0].match(/^file:\/\/\//)) {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, "$1:///");
} else {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, "$1://");
}

for (let i = 0; i < strArray.length; i++) {
let component = strArray[i];

if (typeof component !== "string") {
throw new TypeError("Url must be a string. Received " + component);
}

if (component === "") {
continue;
}

if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, "");
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, "");
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, "/");
}

resultArray.push(component);
}

let str = resultArray.join("/");
// Each input component is now separated by a single slash except the possible first plain protocol part.

// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, "$1");

// replace ? in parameters with &
const parts = str.split("?");
str = parts.shift() + (parts.length > 0 ? "?" : "") + parts.join("&");

return str;
}

export default function urlJoin(...args: string[]): string {
const parts = Array.from(Array.isArray(args[0]) ? args[0] : args);
return normalize(parts);
}

0 comments on commit 72d01f3

Please sign in to comment.