mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
refactor(server): config system (#11081)
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import { defineStartupConfig, ModuleConfig } from '../../base/config';
|
||||
import { defineModuleConfig } from '../../base';
|
||||
|
||||
export interface WorkerStartupConfigurations {
|
||||
allowedOrigin: string[];
|
||||
}
|
||||
|
||||
declare module '../config' {
|
||||
interface PluginsConfig {
|
||||
worker: ModuleConfig<WorkerStartupConfigurations>;
|
||||
declare global {
|
||||
interface AppConfigSchema {
|
||||
worker: {
|
||||
allowedOrigin: ConfigItem<string[]>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
defineStartupConfig('plugins.worker', {
|
||||
allowedOrigin: ['localhost', '127.0.0.1'],
|
||||
defineModuleConfig('worker', {
|
||||
allowedOrigin: {
|
||||
desc: 'Allowed origin',
|
||||
default: ['localhost', '127.0.0.1'],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -10,8 +10,9 @@ import {
|
||||
import type { Request, Response } from 'express';
|
||||
import { HTMLRewriter } from 'htmlrewriter';
|
||||
|
||||
import { BadRequest, Cache, Config, URLHelper } from '../../base';
|
||||
import { BadRequest, Cache, URLHelper, UseNamedGuard } from '../../base';
|
||||
import { Public } from '../../core/auth';
|
||||
import { WorkerService } from './service';
|
||||
import type { LinkPreviewRequest, LinkPreviewResponse } from './types';
|
||||
import {
|
||||
appendUrl,
|
||||
@@ -20,7 +21,6 @@ import {
|
||||
getCorsHeaders,
|
||||
isOriginAllowed,
|
||||
isRefererAllowed,
|
||||
OriginRules,
|
||||
parseJson,
|
||||
reduceUrls,
|
||||
} from './utils';
|
||||
@@ -30,22 +30,19 @@ import { decodeWithCharset } from './utils/encoding';
|
||||
const CACHE_TTL = 1000 * 60 * 30;
|
||||
|
||||
@Public()
|
||||
@UseNamedGuard('selfhost')
|
||||
@Controller('/api/worker')
|
||||
export class WorkerController {
|
||||
private readonly logger = new Logger(WorkerController.name);
|
||||
private readonly allowedOrigin: OriginRules;
|
||||
|
||||
constructor(
|
||||
config: Config,
|
||||
private readonly cache: Cache,
|
||||
private readonly url: URLHelper
|
||||
) {
|
||||
this.allowedOrigin = [
|
||||
...config.plugins.worker.allowedOrigin
|
||||
.map(u => fixUrl(u)?.origin as string)
|
||||
.filter(v => !!v),
|
||||
url.origin,
|
||||
];
|
||||
private readonly url: URLHelper,
|
||||
private readonly service: WorkerService
|
||||
) {}
|
||||
|
||||
private get allowedOrigin() {
|
||||
return this.service.allowedOrigins;
|
||||
}
|
||||
|
||||
@Get('/image-proxy')
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import './config';
|
||||
|
||||
import { Plugin } from '../registry';
|
||||
import { WorkerController } from './controller';
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
@Plugin({
|
||||
name: 'worker',
|
||||
import { WorkerController } from './controller';
|
||||
import { WorkerService } from './service';
|
||||
@Module({
|
||||
providers: [WorkerService],
|
||||
controllers: [WorkerController],
|
||||
if: config => config.isSelfhosted || config.node.dev || config.node.test,
|
||||
})
|
||||
export class WorkerModule {}
|
||||
|
||||
31
packages/backend/server/src/plugins/worker/service.ts
Normal file
31
packages/backend/server/src/plugins/worker/service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { Config, OnEvent, URLHelper } from '../../base';
|
||||
import { fixUrl, OriginRules } from './utils';
|
||||
|
||||
@Injectable()
|
||||
export class WorkerService {
|
||||
allowedOrigins: OriginRules = [this.url.origin];
|
||||
|
||||
constructor(
|
||||
private readonly config: Config,
|
||||
private readonly url: URLHelper
|
||||
) {}
|
||||
|
||||
@OnEvent('config.init')
|
||||
onConfigInit() {
|
||||
this.allowedOrigins = [
|
||||
...this.config.worker.allowedOrigin
|
||||
.map(u => fixUrl(u)?.origin as string)
|
||||
.filter(v => !!v),
|
||||
this.url.origin,
|
||||
];
|
||||
}
|
||||
|
||||
@OnEvent('config.changed')
|
||||
onConfigChanged(event: Events['config.changed']) {
|
||||
if ('worker' in event.updates) {
|
||||
this.onConfigInit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user