core: support random picking cdn and ws endpoints

This commit is contained in:
undefined committed 2024-04-06 05:37:52 +08:00
1 parent 5519fc4270
commit 3ad9d5e614
2 files changed
+11 -2

No files matched your search

+7 -2
View File
@@ -1,6 +1,7 @@
import { PassThrough } from 'stream';
import type { Next } from 'koa';
import { cloneDeep, omit, pick } from 'lodash';
import { randomPick } from '@hydrooj/utils';
import { PERM } from '../../model/builtin';
import * as system from '../../model/system';
import token from '../../model/token';
@@ -59,8 +60,12 @@ export default async (ctx: KoaContext, next: Next) => {
domainId, ...ctx.params, ...ctx.query, ...ctx.request.body, __start: Date.now(),
};
const UiContext: any = cloneDeep(UiContextBase);
if (!process.env.DEV) UiContext.cdn_prefix = system.get('server.cdn');
if (!process.env.DEV) UiContext.ws_prefix = system.get('server.ws');
if (!process.env.DEV) {
UiContext.cdn_prefix = system.get('server.cdn');
if (UiContext.cdn_prefix.includes(',')) UiContext.cdn_prefix = randomPick(UiContext.cdn_prefix.split(','));
UiContext.ws_prefix = system.get('server.ws');
if (UiContext.ws_prefix.includes(',')) UiContext.ws_prefix = randomPick(UiContext.ws_prefix.split(','));
}
UiContext.domainId = domainId;
UiContext.domain = domainInfo;
ctx.HydroContext = {
+4
View File
@@ -199,6 +199,10 @@ export function size(s: number, base = 1) {
return `${Math.round(s * unit)} ${unitNames[unitNames.length - 1]}`;
}
export function randomPick<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}
export type StringKeys<O> = {
[K in keyof O]: string extends O[K] ? K : never
}[keyof O];