Skip to content

Commit

Permalink
Tidy up pass
Browse files Browse the repository at this point in the history
  • Loading branch information
foot committed Sep 7, 2023
1 parent 0cb9826 commit db0a707
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 428 deletions.
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if advanced_go_dev_mode:
'./bin',
],
dockerfile="dev.dockerfile",
entrypoint="/app/build/gitops-server --log-level=debug --insecure --route-prefix=foo",
entrypoint="/app/build/gitops-server --log-level=debug --insecure",
live_update=[
sync('./bin', '/app/build'),
],
Expand Down
4 changes: 2 additions & 2 deletions cmd/gitops-server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ func createRedirector(fsys fs.FS, log logr.Logger, routePrefix string) http.Hand
baseHref = "/" + baseHref
}
if !strings.HasSuffix(baseHref, "/") {
baseHref = baseHref + "/"
baseHref += "/"
}
bt = []byte(strings.Replace(string(bt), "<head>", fmt.Sprintf("<head><base href=\"%s\">", baseHref), 1))
bt = []byte(strings.Replace(string(bt), "<head>", fmt.Sprintf("<head><base href=%q>", baseHref), 1))
}

_, err = w.Write(bt)
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"typedefs": "tsc --declaration --skipLibCheck --emitDeclarationOnly --outDir dist -p .",
"start": "parcel serve --port 4567 ui/index.html",
"lint": "eslint ui --max-warnings 0",
"preview": "node ui/server.js",
"prettify:check": "prettier --check ui",
"prettify:format": "prettier --write ui",
"test": "jest",
Expand Down Expand Up @@ -107,7 +106,6 @@
"buffer": "^5.7.1",
"eslint": "^7.28.0",
"eslint-plugin-import": "^2.25.4",
"express": "^4.18.2",
"jest": "^27.3.1",
"jest-styled-components": "^7.0.4",
"parcel": "^2.8.3",
Expand Down
1 change: 0 additions & 1 deletion ui/components/SubRouterTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function RouterTab({ children }: TabProps) {
}

function SubRouterTabs({ className, children, rootPath, clearQuery }: Props) {
// const location = useLocation
const query = qs.parse(window.location.search);
const childs = findChildren(children);

Expand Down
4 changes: 2 additions & 2 deletions ui/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import qs from "query-string";
import * as React from "react";
import { Redirect, useHistory, useLocation } from "react-router-dom";
import { withBaseURL } from "../lib/utils";
import { reloadBrowserSignIn } from "../lib/utils";
import { AppContext } from "./AppContext";

export enum AuthRoutes {
Expand Down Expand Up @@ -111,7 +111,7 @@ export default function AuthContextProvider({ children }) {
setError(response);
return;
}
window.location.replace(withBaseURL(AuthRoutes.AUTH_PATH_SIGNIN));
reloadBrowserSignIn();
})
.finally(() => setLoading(false));
}, []);
Expand Down
23 changes: 11 additions & 12 deletions ui/contexts/CoreClientContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import qs from "query-string";
import * as React from "react";
import { useQuery } from "react-query";
import { Core, GetFeatureFlagsResponse } from "../lib/api/core/core.pb";
import { TokenRefreshWrapper } from "../lib/requests";
import { RequestError } from "../lib/types";
import { getBaseURL, stripBaseURL, withBaseURL } from "../lib/utils";
import { AuthRoutes } from "./AuthContext";
import {
getBaseURL,
reloadBrowserSignIn,
stripBaseURL,
withBaseURL,
} from "../lib/utils";

type Props = {
api: typeof Core;
Expand Down Expand Up @@ -37,13 +40,9 @@ function FeatureFlags(api) {
export async function refreshToken() {
const res = await fetch(withBaseURL("/oauth2/refresh"), { method: "POST" });
if (!res.ok) {
window.location.replace(
withBaseURL(AuthRoutes.AUTH_PATH_SIGNIN) +
"?" +
qs.stringify({
redirect: stripBaseURL(location.pathname + location.search),
})
);
// The login redirect system is aware of the base URL and will add it,
// so we need to strip it off here.
reloadBrowserSignIn(stripBaseURL(location.pathname) + location.search);

// Return a promse that does not resolve.
// This stops any more API requests or refreshToken requests from being
Expand All @@ -56,7 +55,7 @@ export function UnAuthorizedInterceptor(api: typeof Core): typeof Core {
return TokenRefreshWrapper.wrap(api, refreshToken);
}

export function setBaseURL(api: any) {
export function setAPIPathPrefix(api: any) {
const wrapped: any = {};
for (const method of Object.getOwnPropertyNames(api)) {
if (typeof api[method] != "function") {
Expand All @@ -72,7 +71,7 @@ export function setBaseURL(api: any) {

export default function CoreClientContextProvider({ api, children }: Props) {
let wrapped = UnAuthorizedInterceptor(api);
wrapped = setBaseURL(api);
wrapped = setAPIPathPrefix(api);

return (
<CoreClientContext.Provider
Expand Down
31 changes: 20 additions & 11 deletions ui/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import _ from "lodash";
import { DateTime } from "luxon";
import qs from "query-string";
import { toast } from "react-toastify";
import styled from "styled-components";
import Flex from "../components/Flex";
import { computeReady, ReadyType } from "../components/KubeStatusIndicator";
import { AppVersion, repoUrl } from "../components/Version";
import { AuthRoutes } from "../contexts/AuthContext";
import { GetVersionResponse } from "../lib/api/core/core.pb";
import { Condition, Kind, ObjectRef } from "./api/core/types.pb";
import { Automation, HelmRelease, Kustomization } from "./objects";
Expand Down Expand Up @@ -261,30 +263,37 @@ export const Fade = styled<any>(Flex)<{
${({ fade }) => fade && "pointer-events: none"};
`;

export const reloadBrowserSignIn = (redirect?: string) => {
let path = withBaseURL(AuthRoutes.AUTH_PATH_SIGNIN);

if (redirect) {
const queryString = qs.stringify({ redirect });
path = `${path}?${queryString}`;
}

// hard reload the browser to wipe anything that might be memory
window.location.replace(path);
};

export const getBaseURL = () => {
const baseElement = document.querySelector("base");
if (baseElement) {
return new URL(document.baseURI).pathname;
const { pathname } = new URL(document.baseURI);
return pathname.replace(/\/$/, "");
}
return "";
};

export function withBaseURL(pathname: string) {
const baseURL = getBaseURL();
const normalizedPath = baseURL.endsWith("/") ? baseURL.slice(0, -1) : baseURL;
return `${normalizedPath}${pathname}`;
return getBaseURL() + pathname;
}

export function stripBaseURL(pathname: string) {
const basePath = getBaseURL();

// Ensure basePath has a leading slash
const normalizedBasePath = basePath.startsWith("/")
? basePath
: "/" + basePath;

if (pathname.startsWith(normalizedBasePath)) {
return pathname.slice(normalizedBasePath.length) || "/";
if (pathname.startsWith(basePath)) {
return pathname.slice(basePath.length);
}

return pathname;
}
75 changes: 0 additions & 75 deletions ui/server.js

This file was deleted.

Loading

0 comments on commit db0a707

Please sign in to comment.