From aa7e322baae733e8ab6442b82bdb5eeb6c036f7e Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 4 Oct 2021 16:32:36 +0800 Subject: [PATCH] feat: add pageproxy page source --- src/modules/pageproxy/pageproxy.controller.ts | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/modules/pageproxy/pageproxy.controller.ts b/src/modules/pageproxy/pageproxy.controller.ts index 897d8661..5c1407e3 100644 --- a/src/modules/pageproxy/pageproxy.controller.ts +++ b/src/modules/pageproxy/pageproxy.controller.ts @@ -1,4 +1,10 @@ -import { Controller, Get, Query, Res } from '@nestjs/common' +import { + Controller, + Get, + InternalServerErrorException, + Query, + Res, +} from '@nestjs/common' import { FastifyReply } from 'fastify' import { API_VERSION } from '~/app.config' import { Cookies } from '~/common/decorator/cookie.decorator' @@ -66,30 +72,36 @@ export class PageProxyController { reply.clearCookie('__gatewayUrl') } - let entry = + const source = (!onlyGithub && - (await this.cacheService.get( - getRedisKey(RedisKeys.AdminPage), - ))) || + (await this.cacheService + .get(getRedisKey(RedisKeys.AdminPage)) + .then((text) => text && { text, from: 'redis' }))) || (await (async () => { const indexEntryUrl = `https://raw.githubusercontent.com/mx-space/admin-next/gh-pages/index.html` - const indexEntryCdnUrl = `https://cdn.jsdelivr.net/gh/mx-space/admin-next@gh-pages/index.html?t=${+new Date()}` + const indexEntryCdnUrl = `https://cdn.jsdelivr.net/gh/mx-space/admin-next@gh-pages/index.html` const tasks = [ // 龟兔赛跑, 乌龟先跑 // eslint-disable-next-line @typescript-eslint/no-empty-function - fetch(indexEntryUrl).then((res) => res.text()), + fetch(indexEntryUrl) + .then((res) => res.text()) + .then((text) => ({ text, from: 'github' })), ] if (!onlyGithub) { tasks.push( - sleep(1000).then(async () => - (await fetch(indexEntryCdnUrl)).text(), - ), + sleep(1000) + .then(async () => (await fetch(indexEntryCdnUrl)).text()) + .then((text) => ({ text, from: 'jsdelivr' })), ) } - return await Promise.any(tasks) + return await Promise.any(tasks).catch((e) => { + const err = '网络连接异常, 所有请求均失败, 无法获取后台入口文件' + reply.type('application/json').status(500).send({ message: err }) + throw new InternalServerErrorException(err) + }) })()) - await this.cacheService.set(getRedisKey(RedisKeys.AdminPage), entry, { + await this.cacheService.set(getRedisKey(RedisKeys.AdminPage), source.text, { ttl: 10 * 60, }) @@ -101,9 +113,11 @@ export class PageProxyController { GATEWAY: gatewayUrl ?? cookies['__gatewayUrl'], } - entry = entry.replace( + const entry = source.text.replace( ``, - `