forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update launchpad network status (labring#5444)
* fix read api * update network * update * update * update style
- Loading branch information
Showing
9 changed files
with
207 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
frontend/providers/applaunchpad/src/components/Icon/icons/loading.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
frontend/providers/applaunchpad/src/hooks/useNetworkStatus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useQueries } from '@tanstack/react-query'; | ||
|
||
interface NetworkStatus { | ||
url: string; | ||
isReady: boolean; | ||
error?: string; | ||
} | ||
|
||
interface ApiResponse { | ||
code: number; | ||
data?: { ready: boolean }; | ||
message?: string; | ||
error?: string; | ||
} | ||
|
||
export const useNetworkStatus = (networks: { inline: string; public: string }[]) => { | ||
const urlPairs = networks | ||
.map((network) => network.public) | ||
.filter(Boolean) | ||
.map((originalUrl) => ({ | ||
originalUrl, | ||
fetchUrl: originalUrl.replace(/^(wss|grpcs):\/\//, 'https://') | ||
})); | ||
|
||
return useQueries({ | ||
queries: urlPairs.map(({ originalUrl, fetchUrl }) => ({ | ||
queryKey: ['networkStatus', originalUrl], | ||
queryFn: async (): Promise<NetworkStatus> => { | ||
const response = await fetch(`/api/check-ready?url=${encodeURIComponent(fetchUrl)}`); | ||
const data: ApiResponse = await response.json(); | ||
if (data.code !== 200) { | ||
throw new Error(data.message || data.error || 'Service not ready'); | ||
} | ||
|
||
return { | ||
url: originalUrl, | ||
isReady: data.data?.ready ?? false | ||
}; | ||
}, | ||
retry: 5, | ||
retryDelay: (attemptIndex: number) => Math.min(1000 * Math.pow(2, attemptIndex), 30000), | ||
refetchIntervalInBackground: false, | ||
staleTime: 1000 * 60 * 5 | ||
})) | ||
} as const); | ||
}; |
46 changes: 46 additions & 0 deletions
46
frontend/providers/applaunchpad/src/pages/api/check-ready.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { authSession } from '@/services/backend/auth'; | ||
import { getK8s } from '@/services/backend/kubernetes'; | ||
import { jsonRes } from '@/services/backend/response'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function handler(request: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
const { url } = request.query as { url: string }; | ||
if (!url) { | ||
return jsonRes(res, { | ||
code: 400, | ||
error: '缺少 URL 参数' | ||
}); | ||
} | ||
|
||
const response = await fetch(url); | ||
|
||
if (response.status === 503) { | ||
return jsonRes(res, { | ||
code: 503, | ||
message: 'Service Unavailable (503)' | ||
}); | ||
} | ||
|
||
const text = await response.text(); | ||
if (text.includes('upstream not health')) { | ||
return jsonRes(res, { | ||
code: 503, | ||
message: 'Upstream not healthy' | ||
}); | ||
} | ||
|
||
return jsonRes(res, { | ||
code: 200, | ||
data: { | ||
ready: true | ||
} | ||
}); | ||
} catch (error: any) { | ||
console.error(error); | ||
return jsonRes(res, { | ||
code: 500, | ||
error: error?.message | ||
}); | ||
} | ||
} |