feat: load program-settings from SuperAdmin with 3-host failover
Use /api/public/program-settings only and retry montzar/montech/montest three rounds before showing the bootstrap error. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+45
-41
@@ -1,3 +1,11 @@
|
||||
import {
|
||||
allowSuperAdminFallback,
|
||||
getSuperAdminBases,
|
||||
isSuperAdminUrl,
|
||||
resolveTenantDomain,
|
||||
SA_RETRY_ROUNDS,
|
||||
} from '@/config/superadmin'
|
||||
|
||||
type VersionResponse =
|
||||
| string
|
||||
| number
|
||||
@@ -9,10 +17,9 @@ type VersionResponse =
|
||||
}
|
||||
|
||||
const VERSION_SUCCESS_URL_KEY = 'VERSION_Success_URL'
|
||||
export const VERSION_RETRY_ROUNDS = 5
|
||||
export const VERSION_RETRY_ROUNDS = SA_RETRY_ROUNDS
|
||||
|
||||
let versionFetchPromise: Promise<string | null> | null = null
|
||||
let cachedVersionResult: string | null | undefined
|
||||
|
||||
function getProtocol() {
|
||||
return window.location.protocol === 'https:' ? 'https:' : 'http:'
|
||||
@@ -20,31 +27,34 @@ function getProtocol() {
|
||||
|
||||
function getVersionUrls() {
|
||||
const protocol = getProtocol()
|
||||
const host = String(window.location.hostname || '').trim()
|
||||
const domain = encodeURIComponent(
|
||||
!host || host === 'localhost' || host === '127.0.0.1' ? 'k.montzar.ir' : host,
|
||||
)
|
||||
const domain = encodeURIComponent(resolveTenantDomain())
|
||||
const urls = getSuperAdminBases().map((base) => `${base}/get_version?domain=${domain}`)
|
||||
|
||||
return [
|
||||
`https://adm.montzar.ir/get_version?domain=${domain}`,
|
||||
`https://adm.montech.ir/get_version?domain=${domain}`,
|
||||
`https://adm.montest.ir/get_version?domain=${domain}`,
|
||||
'https://tablo.talasevin.ir/get_version',
|
||||
'https://abshode-service.montgit.ir/server/get_version',
|
||||
`${protocol}//212.23.201.173:8381/get_version`,
|
||||
`${protocol}//91.212.174.137:4040/server/get_version`,
|
||||
]
|
||||
if (allowSuperAdminFallback()) {
|
||||
urls.push(
|
||||
'https://tablo.talasevin.ir/get_version',
|
||||
'https://abshode-service.montgit.ir/server/get_version',
|
||||
`${protocol}//212.23.201.173:8381/get_version`,
|
||||
`${protocol}//91.212.174.137:4040/server/get_version`,
|
||||
)
|
||||
}
|
||||
|
||||
return urls
|
||||
}
|
||||
|
||||
function sortVersionUrls(urls: string[]) {
|
||||
const lastSuccessUrl = localStorage.getItem(VERSION_SUCCESS_URL_KEY)
|
||||
|
||||
if (!lastSuccessUrl) {
|
||||
return urls
|
||||
if (
|
||||
!lastSuccessUrl ||
|
||||
!urls.includes(lastSuccessUrl) ||
|
||||
!isSuperAdminUrl(lastSuccessUrl)
|
||||
) {
|
||||
if (lastSuccessUrl && !isSuperAdminUrl(lastSuccessUrl)) {
|
||||
localStorage.removeItem(VERSION_SUCCESS_URL_KEY)
|
||||
}
|
||||
return [...urls]
|
||||
}
|
||||
|
||||
const others = urls.filter((url) => url !== lastSuccessUrl)
|
||||
return [lastSuccessUrl, ...others]
|
||||
return [lastSuccessUrl, ...urls.filter((url) => url !== lastSuccessUrl)]
|
||||
}
|
||||
|
||||
function clearStaleVersionSuccessUrl(url: string) {
|
||||
@@ -95,10 +105,10 @@ async function fetchVersion(url: string, timeout = 3500): Promise<string | null>
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
Accept: 'application/json',
|
||||
},
|
||||
cache: 'no-store',
|
||||
signal: controller.signal
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -106,18 +116,22 @@ async function fetchVersion(url: string, timeout = 3500): Promise<string | null>
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') ?? ''
|
||||
if (!contentType.includes('application/json')) {
|
||||
throw new Error('Version response is not JSON')
|
||||
}
|
||||
|
||||
const data = contentType.includes('application/json')
|
||||
? ((await response.json()) as VersionResponse)
|
||||
: ((await response.text()) as VersionResponse)
|
||||
|
||||
const data = (await response.json()) as VersionResponse
|
||||
const version = normalizeVersionResponse(data)
|
||||
|
||||
if (!version) {
|
||||
throw new Error('Version response is empty')
|
||||
}
|
||||
|
||||
localStorage.setItem(VERSION_SUCCESS_URL_KEY, url)
|
||||
if (isSuperAdminUrl(url)) {
|
||||
localStorage.setItem(VERSION_SUCCESS_URL_KEY, url)
|
||||
} else {
|
||||
clearStaleVersionSuccessUrl(url)
|
||||
}
|
||||
|
||||
return version
|
||||
} finally {
|
||||
@@ -149,7 +163,6 @@ async function fetchVersionWithRetry(rounds = VERSION_RETRY_ROUNDS): Promise<str
|
||||
}
|
||||
|
||||
export function resetVersionCheckCache() {
|
||||
cachedVersionResult = undefined
|
||||
versionFetchPromise = null
|
||||
}
|
||||
|
||||
@@ -164,22 +177,13 @@ export async function loadRemoteAppVersion(options?: {
|
||||
resetVersionCheckCache()
|
||||
}
|
||||
|
||||
if (cachedVersionResult !== undefined) {
|
||||
return cachedVersionResult
|
||||
}
|
||||
|
||||
if (versionFetchPromise) {
|
||||
return versionFetchPromise
|
||||
}
|
||||
|
||||
versionFetchPromise = fetchVersionWithRetry(rounds)
|
||||
.then((version) => {
|
||||
cachedVersionResult = version
|
||||
return version
|
||||
})
|
||||
.finally(() => {
|
||||
versionFetchPromise = null
|
||||
})
|
||||
versionFetchPromise = fetchVersionWithRetry(rounds).finally(() => {
|
||||
versionFetchPromise = null
|
||||
})
|
||||
|
||||
return versionFetchPromise
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user