From 27b5f55ef91f9eb7d3cfde53620ddcd83ec629cd Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62335616+lucaseduoli@users.noreply.github.com> Date: Fri, 30 Aug 2024 17:40:16 -0300 Subject: [PATCH] refactor: health check endpoint (#3635) * Changed health check to use /health_check and to be modularized * Added default value * Added health constant to default export * check cors before inserting custom headers * Added get global variables check on init * Updated global variables to start with undefined * Fixed redirect when showGeneralSettings is disabled * Added isOpen props to custom api generator --- .../src/components/authSettingsGuard/index.tsx | 2 +- src/frontend/src/controllers/API/api.tsx | 10 ++++++++-- .../controllers/API/queries/health/use-get-health.ts | 5 ++++- .../customization/components/custom-api-generator.tsx | 2 +- src/frontend/src/customization/config-constants.ts | 2 ++ src/frontend/src/modals/apiModal/index.tsx | 2 +- src/frontend/src/pages/AppInitPage/index.tsx | 2 ++ src/frontend/src/pages/FlowPage/index.tsx | 2 -- src/frontend/src/pages/Playground/index.tsx | 2 -- src/frontend/src/pages/ViewPage/index.tsx | 2 -- .../src/stores/globalVariablesStore/globalVariables.ts | 2 +- .../src/types/zustand/globalVariables/index.ts | 2 +- 12 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/frontend/src/components/authSettingsGuard/index.tsx b/src/frontend/src/components/authSettingsGuard/index.tsx index 2c7d53526..260c0a0d5 100644 --- a/src/frontend/src/components/authSettingsGuard/index.tsx +++ b/src/frontend/src/components/authSettingsGuard/index.tsx @@ -13,6 +13,6 @@ export const AuthSettingsGuard = ({ children }) => { if (showGeneralSettings) { return children; } else { - return ; + return ; } }; diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index f5c20314b..425455513 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -103,8 +103,14 @@ function ApiInterceptor() { config.headers["Authorization"] = `Bearer ${accessToken}`; } - for (const [key, value] of Object.entries(customHeaders)) { - config.headers[key] = value; + const currentOrigin = window.location.origin; + const requestUrl = new URL(config?.url as string, currentOrigin); + + const urlIsFromCurrentOrigin = requestUrl.origin === currentOrigin; + if (urlIsFromCurrentOrigin) { + for (const [key, value] of Object.entries(customHeaders)) { + config.headers[key] = value; + } } return { diff --git a/src/frontend/src/controllers/API/queries/health/use-get-health.ts b/src/frontend/src/controllers/API/queries/health/use-get-health.ts index 044cecc65..908a71ced 100644 --- a/src/frontend/src/controllers/API/queries/health/use-get-health.ts +++ b/src/frontend/src/controllers/API/queries/health/use-get-health.ts @@ -2,6 +2,7 @@ import { REFETCH_SERVER_HEALTH_INTERVAL, SERVER_HEALTH_INTERVAL, } from "@/constants/constants"; +import { HEALTH_CHECK_URL } from "@/customization/config-constants"; import { useUtilityStore } from "@/stores/utilityStore"; import { createNewError503 } from "@/types/factory/axios-error-503"; import { keepPreviousData } from "@tanstack/react-query"; @@ -44,7 +45,9 @@ export const useGetHealthQuery: useQueryFunctionType< setTimeout(() => reject(createNewError503()), SERVER_HEALTH_INTERVAL), ); - const apiPromise = api.get("/health"); + const apiPromise = api.get( + HEALTH_CHECK_URL || "/health", + ); const response = await Promise.race([apiPromise, timeoutPromise]); setHealthCheckTimeout( Object.values(response.data).some((value) => value !== "ok") diff --git a/src/frontend/src/customization/components/custom-api-generator.tsx b/src/frontend/src/customization/components/custom-api-generator.tsx index d6d6bb697..c01a2e341 100644 --- a/src/frontend/src/customization/components/custom-api-generator.tsx +++ b/src/frontend/src/customization/components/custom-api-generator.tsx @@ -1,3 +1,3 @@ -export function CustomAPIGenerator() { +export function CustomAPIGenerator({ isOpen }: { isOpen: boolean }) { return <>; } diff --git a/src/frontend/src/customization/config-constants.ts b/src/frontend/src/customization/config-constants.ts index 1f4a28a59..c8f401d00 100644 --- a/src/frontend/src/customization/config-constants.ts +++ b/src/frontend/src/customization/config-constants.ts @@ -3,6 +3,7 @@ export const PORT = 3000; export const PROXY_TARGET = "http://127.0.0.1:7860"; export const API_ROUTES = ["^/api/v1/", "/health"]; export const BASE_URL_API = "/api/v1/"; +export const HEALTH_CHECK_URL = "/health_check"; export default { BASENAME, @@ -10,4 +11,5 @@ export default { PROXY_TARGET, API_ROUTES, BASE_URL_API, + HEALTH_CHECK_URL, }; diff --git a/src/frontend/src/modals/apiModal/index.tsx b/src/frontend/src/modals/apiModal/index.tsx index 2be3e1ced..1cc050994 100644 --- a/src/frontend/src/modals/apiModal/index.tsx +++ b/src/frontend/src/modals/apiModal/index.tsx @@ -54,7 +54,7 @@ export default function ApiModal({ /> - + { if (isFetched) { diff --git a/src/frontend/src/pages/FlowPage/index.tsx b/src/frontend/src/pages/FlowPage/index.tsx index 9c622ded5..f3cb66acc 100644 --- a/src/frontend/src/pages/FlowPage/index.tsx +++ b/src/frontend/src/pages/FlowPage/index.tsx @@ -1,5 +1,4 @@ import { useGetRefreshFlows } from "@/controllers/API/queries/flows/use-get-refresh-flows"; -import { useGetGlobalVariables } from "@/controllers/API/queries/variables"; import { ENABLE_BRANDING } from "@/customization/feature-flags"; import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate"; import useSaveFlow from "@/hooks/flows/use-save-flow"; @@ -29,7 +28,6 @@ export default function FlowPage({ view }: { view?: boolean }): JSX.Element { const setOnFlowPage = useFlowStore((state) => state.setOnFlowPage); const { id } = useParams(); const navigate = useCustomNavigate(); - useGetGlobalVariables(); const saveFlow = useSaveFlow(); const flows = useFlowsManagerStore((state) => state.flows); diff --git a/src/frontend/src/pages/Playground/index.tsx b/src/frontend/src/pages/Playground/index.tsx index 211a32f41..a7fbc7225 100644 --- a/src/frontend/src/pages/Playground/index.tsx +++ b/src/frontend/src/pages/Playground/index.tsx @@ -1,5 +1,4 @@ import { useGetRefreshFlows } from "@/controllers/API/queries/flows/use-get-refresh-flows"; -import { useGetGlobalVariables } from "@/controllers/API/queries/variables"; import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate"; import { track } from "@/customization/utils/analytics"; import { useStoreStore } from "@/stores/storeStore"; @@ -25,7 +24,6 @@ export default function PlaygroundPage() { } const navigate = useCustomNavigate(); - useGetGlobalVariables(); const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId); const { mutateAsync: refreshFlows } = useGetRefreshFlows(); diff --git a/src/frontend/src/pages/ViewPage/index.tsx b/src/frontend/src/pages/ViewPage/index.tsx index c5bf14fc4..ae33879a1 100644 --- a/src/frontend/src/pages/ViewPage/index.tsx +++ b/src/frontend/src/pages/ViewPage/index.tsx @@ -1,5 +1,4 @@ import { useGetRefreshFlows } from "@/controllers/API/queries/flows/use-get-refresh-flows"; -import { useGetGlobalVariables } from "@/controllers/API/queries/variables"; import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate"; import { useTypesStore } from "@/stores/typesStore"; import { useEffect } from "react"; @@ -12,7 +11,6 @@ export default function ViewPage() { const { id } = useParams(); const navigate = useCustomNavigate(); - useGetGlobalVariables(); const flows = useFlowsManagerStore((state) => state.flows); const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId); diff --git a/src/frontend/src/stores/globalVariablesStore/globalVariables.ts b/src/frontend/src/stores/globalVariablesStore/globalVariables.ts index 035d45727..8096a6b9b 100644 --- a/src/frontend/src/stores/globalVariablesStore/globalVariables.ts +++ b/src/frontend/src/stores/globalVariablesStore/globalVariables.ts @@ -7,7 +7,7 @@ export const useGlobalVariablesStore = create( setUnavailableFields: (fields) => { set({ unavailableFields: fields }); }, - globalVariablesEntries: [], + globalVariablesEntries: undefined, setGlobalVariablesEntries: (entries) => { set({ globalVariablesEntries: entries }); }, diff --git a/src/frontend/src/types/zustand/globalVariables/index.ts b/src/frontend/src/types/zustand/globalVariables/index.ts index 12a0d4469..81e10993e 100644 --- a/src/frontend/src/types/zustand/globalVariables/index.ts +++ b/src/frontend/src/types/zustand/globalVariables/index.ts @@ -1,5 +1,5 @@ export type GlobalVariablesStore = { - globalVariablesEntries: Array; + globalVariablesEntries: Array | undefined; setGlobalVariablesEntries: (entries: Array) => void; unavailableFields: { [name: string]: string }; setUnavailableFields: (fields: { [name: string]: string }) => void;