mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 19:16:29 +08:00
refactor(server): config system (#11081)
This commit is contained in:
@@ -1,11 +1,54 @@
|
||||
import { RedisOptions } from 'ioredis';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { defineStartupConfig, ModuleConfig } from '../../base/config';
|
||||
import { defineModuleConfig } from '../config';
|
||||
|
||||
declare module '../config' {
|
||||
interface AppConfig {
|
||||
redis: ModuleConfig<RedisOptions>;
|
||||
declare global {
|
||||
interface AppConfigSchema {
|
||||
redis: {
|
||||
host: string;
|
||||
port: number;
|
||||
db: number;
|
||||
username: string;
|
||||
password: string;
|
||||
ioredis: ConfigItem<
|
||||
Omit<RedisOptions, 'host' | 'port' | 'db' | 'username' | 'password'>
|
||||
>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
defineStartupConfig('redis', {});
|
||||
defineModuleConfig('redis', {
|
||||
db: {
|
||||
desc: 'The database index of redis server to be used(Must be less than 10).',
|
||||
default: 0,
|
||||
env: ['REDIS_DATABASE', 'integer'],
|
||||
validate: val => val >= 0 && val < 10,
|
||||
},
|
||||
host: {
|
||||
desc: 'The host of the redis server.',
|
||||
default: 'localhost',
|
||||
env: ['REDIS_HOST', 'string'],
|
||||
},
|
||||
port: {
|
||||
desc: 'The port of the redis server.',
|
||||
default: 6379,
|
||||
env: ['REDIS_PORT', 'integer'],
|
||||
shape: z.number().positive(),
|
||||
},
|
||||
username: {
|
||||
desc: 'The username of the redis server.',
|
||||
default: '',
|
||||
env: ['REDIS_USERNAME', 'string'],
|
||||
},
|
||||
password: {
|
||||
desc: 'The password of the redis server.',
|
||||
default: '',
|
||||
env: ['REDIS_PASSWORD', 'string'],
|
||||
},
|
||||
ioredis: {
|
||||
desc: 'The config for the ioredis client.',
|
||||
default: {},
|
||||
link: 'https://github.com/luin/ioredis',
|
||||
},
|
||||
});
|
||||
|
||||
@@ -6,13 +6,10 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { Redis as IORedis, RedisOptions } from 'ioredis';
|
||||
|
||||
import { Config } from '../../base/config';
|
||||
import { Config } from '../config';
|
||||
|
||||
class Redis extends IORedis implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(this.constructor.name);
|
||||
constructor(opts: RedisOptions) {
|
||||
super(opts);
|
||||
}
|
||||
|
||||
errorHandler = (err: Error) => {
|
||||
this.logger.error(err);
|
||||
@@ -46,21 +43,29 @@ class Redis extends IORedis implements OnModuleInit, OnModuleDestroy {
|
||||
@Injectable()
|
||||
export class CacheRedis extends Redis {
|
||||
constructor(config: Config) {
|
||||
super(config.redis);
|
||||
super({ ...config.redis, ...config.redis.ioredis });
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class SessionRedis extends Redis {
|
||||
constructor(config: Config) {
|
||||
super({ ...config.redis, db: (config.redis.db ?? 0) + 2 });
|
||||
super({
|
||||
...config.redis,
|
||||
...config.redis.ioredis,
|
||||
db: (config.redis.db ?? 0) + 2,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class SocketIoRedis extends Redis {
|
||||
constructor(config: Config) {
|
||||
super({ ...config.redis, db: (config.redis.db ?? 0) + 3 });
|
||||
super({
|
||||
...config.redis,
|
||||
...config.redis.ioredis,
|
||||
db: (config.redis.db ?? 0) + 3,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +74,7 @@ export class QueueRedis extends Redis {
|
||||
constructor(config: Config) {
|
||||
super({
|
||||
...config.redis,
|
||||
...config.redis.ioredis,
|
||||
db: (config.redis.db ?? 0) + 4,
|
||||
// required explicitly set to `null` by bullmq
|
||||
maxRetriesPerRequest: null,
|
||||
|
||||
Reference in New Issue
Block a user