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:
@@ -0,0 +1,83 @@
|
||||
/** سوپرادمین — مستر، بعد دو اسلیو */
|
||||
export const SUPERADMIN_BASES = [
|
||||
'https://adm.montzar.ir',
|
||||
'https://adm.montech.ir',
|
||||
'https://adm.montest.ir',
|
||||
] as const
|
||||
|
||||
/** هر API: سه دامنه × سه دور */
|
||||
export const SA_RETRY_ROUNDS = 3
|
||||
|
||||
/** تنظیمات پیشرفته نسخه — همان دادهٔ /get_settings بدون نیاز به nginx جدا */
|
||||
export const SA_PROGRAM_SETTINGS_PATH = '/api/public/program-settings'
|
||||
|
||||
function normalizeBase(url: string) {
|
||||
return String(url || '').trim().replace(/\/$/, '')
|
||||
}
|
||||
|
||||
export function getSuperAdminBases(): string[] {
|
||||
const fromEnv = normalizeBase(String(import.meta.env.VITE_SUPERADMIN_URL || ''))
|
||||
const bases = [...SUPERADMIN_BASES]
|
||||
if (fromEnv) {
|
||||
return [fromEnv, ...bases.filter((base) => base !== fromEnv)]
|
||||
}
|
||||
return bases
|
||||
}
|
||||
|
||||
export function getSuperAdminBase(): string {
|
||||
return getSuperAdminBases()[0] || ''
|
||||
}
|
||||
|
||||
export function resolveTenantDomain(): string {
|
||||
const override = String(import.meta.env.VITE_TENANT_DOMAIN || '').trim()
|
||||
if (override) return override
|
||||
|
||||
const host = String(window.location.hostname || '').trim()
|
||||
if (!host || host === 'localhost' || host === '127.0.0.1') {
|
||||
return 'k.montzar.ir'
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
export function isSuperAdminUrl(url: string): boolean {
|
||||
return getSuperAdminBases().some((base) => url.startsWith(base))
|
||||
}
|
||||
|
||||
export function allowSuperAdminFallback(): boolean {
|
||||
return Boolean(import.meta.env.DEV)
|
||||
}
|
||||
|
||||
export async function fetchSuperAdminJson<T>(
|
||||
buildUrl: (base: string) => string,
|
||||
timeout = 5000,
|
||||
): Promise<T> {
|
||||
let lastError: unknown
|
||||
const bases = getSuperAdminBases()
|
||||
for (let round = 1; round <= SA_RETRY_ROUNDS; round++) {
|
||||
for (const base of bases) {
|
||||
const controller = new AbortController()
|
||||
const timer = window.setTimeout(() => controller.abort(), timeout)
|
||||
try {
|
||||
const res = await fetch(buildUrl(base), {
|
||||
method: 'GET',
|
||||
cache: 'no-store',
|
||||
signal: controller.signal,
|
||||
headers: { Accept: 'application/json' },
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
const contentType = res.headers.get('content-type') || ''
|
||||
if (!contentType.includes('application/json')) {
|
||||
throw new Error('SuperAdmin response is not JSON')
|
||||
}
|
||||
return (await res.json()) as T
|
||||
} catch (error) {
|
||||
lastError = error
|
||||
} finally {
|
||||
window.clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
}
|
||||
throw lastError instanceof Error ? lastError : new Error('SuperAdmin unreachable')
|
||||
}
|
||||
Reference in New Issue
Block a user