feat: 修改为bun,优化代码
This commit is contained in:
@@ -2,10 +2,11 @@ import path from 'path';
|
||||
import dotenv from 'dotenv';
|
||||
// import { useConfig } from '@kevisual/use-config/env';
|
||||
|
||||
const envFiles = [
|
||||
export const envFiles = [
|
||||
path.resolve(process.cwd(), process.env.NODE_ENV === 'development' ? '.env.dev' : '.env'),
|
||||
// path.resolve(process.cwd(), '.env'), //
|
||||
];
|
||||
console.log('envFiles', envFiles);
|
||||
export const config = dotenv.config({
|
||||
path: envFiles,
|
||||
override: true,
|
||||
|
||||
@@ -1,18 +1,29 @@
|
||||
import { Redis } from 'ioredis';
|
||||
|
||||
import { config } from './config.ts';
|
||||
const redisConfig = {
|
||||
host: config.REDIS_HOST || 'localhost',
|
||||
port: parseInt(config.REDIS_PORT || '6379'),
|
||||
password: config.REDIS_PASSWORD,
|
||||
};
|
||||
export const createRedisClient = (options = {}) => {
|
||||
const redisClient = new Redis({
|
||||
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||
port: 6379, // Redis 服务器的端口号
|
||||
// password: 'your_password', // Redis 的密码 (如果有)
|
||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||
keyPrefix: '', // key 前缀
|
||||
retryStrategy(times) {
|
||||
// 连接重试策略
|
||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||
},
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
...redisConfig,
|
||||
...options,
|
||||
});
|
||||
return redisClient;
|
||||
};
|
||||
// 配置 Redis 连接
|
||||
export const redis = new Redis({
|
||||
host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||
port: 6379, // Redis 服务器的端口号
|
||||
// password: 'your_password', // Redis 的密码 (如果有)
|
||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||
keyPrefix: '', // key 前缀
|
||||
retryStrategy(times) {
|
||||
// 连接重试策略
|
||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||
},
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
});
|
||||
export const redis = createRedisClient();
|
||||
|
||||
// 监听连接事件
|
||||
redis.on('connect', () => {
|
||||
@@ -24,5 +35,5 @@ redis.on('error', (err) => {
|
||||
});
|
||||
|
||||
// 初始化 Redis 客户端
|
||||
export const redisPublisher = new Redis(); // 用于发布消息
|
||||
export const redisSubscriber = new Redis(); // 用于订阅消息
|
||||
export const redisPublisher = createRedisClient(); // 用于发布消息
|
||||
export const redisSubscriber = createRedisClient(); // 用于订阅消息
|
||||
|
||||
@@ -11,7 +11,8 @@ export type PostgresConfig = {
|
||||
};
|
||||
};
|
||||
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
|
||||
console.error('postgres config is required password and user');
|
||||
log.error('postgres config is required password and user');
|
||||
log.error('config', config);
|
||||
process.exit(1);
|
||||
}
|
||||
const postgresConfig = {
|
||||
|
||||
Reference in New Issue
Block a user