core&ui: add setting provider

This commit is contained in:
undefined committed 2024-01-15 15:58:28 +08:00
1 parent 886fa6821d
commit 5d2ac74b04
10 files changed
+56 -28

No files matched your search

+23 -7
View File
@@ -23,6 +23,7 @@ import { Types } from '../lib/validator';
import { Logger } from '../logger';
import { PERM, PRIV } from '../model/builtin';
import * as opcount from '../model/opcount';
import * as OplogModel from '../model/oplog';
import * as system from '../model/system';
import { User } from '../model/user';
import { builtinConfig } from '../settings';
@@ -119,9 +120,9 @@ export class HandlerCommon {
url: (name: string, args?: any) => string;
translate: (key: string) => string;
session: Record<string, any>;
ctx: Context;
/** @deprecated */
domainId: string;
ctx: Context = global.app;
constructor(
public context: KoaContext, public readonly args: Record<string, any>,
@@ -134,6 +135,9 @@ export class HandlerCommon {
this.translate = context.translate.bind(context);
this.session = context.session;
this.domainId = args.domainId;
this.ctx = global.app.extend({
domain: this.domain,
});
}
async limitRate(op: string, periodSecs: number, maxOperations: number, withUserId = system.get('limit.by_user')) {
@@ -243,10 +247,11 @@ async function handle(ctx: KoaContext, HandlerClass, checker) {
args, request, response, user, domain, UiContext,
} = ctx.HydroContext;
Object.assign(args, ctx.params);
const init = Date.now();
const h = new HandlerClass(ctx, args, request, response, user, domain, UiContext);
ctx.handler = h;
const method = ctx.method.toLowerCase();
try {
const method = ctx.method.toLowerCase();
const operation = (method === 'post' && ctx.request.body?.operation)
? `_${ctx.request.body.operation}`.replace(/_([a-z])/gm, (s) => s[1].toUpperCase())
: '';
@@ -307,8 +312,16 @@ async function handle(ctx: KoaContext, HandlerClass, checker) {
await h.onerror(e);
} catch (err) {
h.response.code = 500;
h.response.type = 'text/plain';
h.response.body = `${err.message}\n${err.stack}`;
}
} finally {
const finish = Date.now();
if (finish - init > 5000) {
const id = await OplogModel.log(h, 'slow_request', { method, processtime: finish - init });
logger.warn(`Slow handler: ID=${id}, `, method, ctx.path, `${finish - init}ms`);
}
// TODO metrics: calc avg response time
}
}
@@ -532,12 +545,15 @@ export async function apply(pluginContext: Context) {
if (process.env.DEV) {
app.use(async (ctx: Koa.Context, next: Function) => {
const startTime = Date.now();
await next();
const endTime = Date.now();
if (ctx.nolog || ctx.response.headers.nolog) return;
ctx._remoteAddress = ctx.request.ip;
logger.debug(`${ctx.request.method} /${ctx.domainId || 'system'}${ctx.request.path} \
try {
await next();
} finally {
const endTime = Date.now();
if (!(ctx.nolog || ctx.response.headers.nolog)) {
logger.debug(`${ctx.request.method} /${ctx.domainId || 'system'}${ctx.request.path} \
${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`);
}
}
});
}
const uploadDir = join(tmpdir(), 'hydro', 'upload', process.env.NODE_APP_INSTANCE || '0');