feat: pageproxy can assign version

This commit is contained in:
Innei
2021-10-04 19:07:37 +08:00
parent 77fc6b4f8d
commit 5c6d92952a
4 changed files with 49 additions and 9 deletions

View File

@@ -16,6 +16,11 @@
"eslint --cache"
]
},
"adminRepository": "mx-space/admin-next",
"repository": {
"directory": "mx-space/server-next",
"url": "https://github.com/mx-space/server-next"
},
"homepage": "https://github.com/mx-space/server-next#readme",
"issues": "https://github.com/mx-space/server-next/issues",
"scripts": {

View File

@@ -61,3 +61,8 @@ exports.SECURITY = {
// 跳过登陆鉴权
skipAuth: !isDev ? true : argv.skip_auth ?? false,
}
exports.ADMIN_DASHBOARD = {
// 需要获取反代管理面板的版本
version: '3.6.10',
}

View File

@@ -2,11 +2,13 @@ import {
Controller,
Get,
InternalServerErrorException,
Logger,
Query,
Res,
} from '@nestjs/common'
import { FastifyReply } from 'fastify'
import { API_VERSION } from '~/app.config'
import PKG from 'package.json'
import { ADMIN_DASHBOARD, API_VERSION } from '~/app.config'
import { Cookies } from '~/common/decorator/cookie.decorator'
import { HTTPDecorators } from '~/common/decorator/http.decorator'
import { ApiName } from '~/common/decorator/openapi.decorator'
@@ -16,7 +18,6 @@ import { getRedisKey } from '~/utils/redis.util'
import { ConfigsService } from '../configs/configs.service'
import { InitService } from '../init/init.service'
import { PageProxyDebugDto } from './pageproxy.dto'
interface IInjectableData {
BASE_API: null | string
WEB_URL: null | string
@@ -57,8 +58,13 @@ export class PageProxyController {
__gatewayUrl: gatewayUrl,
__onlyGithub: onlyGithub,
__debug: debug,
__version: adminVersion = ADMIN_DASHBOARD.version,
__purge,
} = query
if (__purge) {
await this.cacheService.getClient().del(getRedisKey(RedisKeys.AdminPage))
}
if (apiUrl) {
reply.setCookie('__apiUrl', apiUrl, { maxAge: 1000 * 60 * 10 })
}
@@ -74,14 +80,25 @@ export class PageProxyController {
const source =
(!onlyGithub &&
!adminVersion &&
(await this.cacheService
.get<string>(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`
let latestVersion = ''
// 没有指定版本, 则获取最新版本
if (!adminVersion) {
// tag_name: v3.6.x
const { tag_name } = await fetch(
`https://api.github.com/repos/${PKG.adminRepository}/releases/latest`,
).then((data) => data.json())
latestVersion = tag_name.replace(/^v/, '')
}
const v = adminVersion || latestVersion
const indexEntryUrl = `https://raw.githubusercontent.com/${PKG.adminRepository}/page_v${v}/index.html`
const indexEntryCdnUrl = `https://cdn.jsdelivr.net/gh/${PKG.adminRepository}@page_v${v}/index.html`
Logger.debug('use admin version: ' + v, 'PageProxy')
const tasks = [
// 龟兔赛跑, 乌龟先跑
// eslint-disable-next-line @typescript-eslint/no-empty-function
fetch(indexEntryUrl)
.then((res) => res.text())
@@ -89,8 +106,8 @@ export class PageProxyController {
]
if (!onlyGithub) {
tasks.push(
sleep(1000)
.then(async () => (await fetch(indexEntryCdnUrl)).text())
fetch(indexEntryCdnUrl)
.then((res) => res.text())
.then((text) => ({ text, from: 'jsdelivr' })),
)
}
@@ -115,7 +132,7 @@ export class PageProxyController {
const entry = source.text.replace(
`<!-- injectable script -->`,
`<script>${`window.page_source='${
`<script>${`window.pageSource='${
source.from
}';\nwindow.injectData = ${JSON.stringify({
LOGIN_BG: adminExtra.background,

View File

@@ -1,5 +1,5 @@
import { Transform } from 'class-transformer'
import { IsBoolean, IsIn, IsOptional, IsUrl } from 'class-validator'
import { IsBoolean, IsIn, IsOptional, IsSemVer, IsUrl } from 'class-validator'
export class PageProxyDebugDto {
@IsIn([false])
@@ -21,4 +21,17 @@ export class PageProxyDebugDto {
* If true, always use index.html pull from github.
*/
__onlyGithub = false
@IsOptional()
@IsSemVer()
@Transform(({ value }) => (value === 'latest' ? null : value))
__version?: string
/**
* 无缓存访问, redis no
*/
@IsBoolean()
@Transform(({ value }) => (value === 'true' ? true : false))
@IsOptional()
__purge = false
}