mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: support random picking cdn and ws endpoints
This commit is contained in:
2 files changed
+11
-2
No files matched your search
@@ -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 = {
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in new issue
Block a user