feat: clear app.config.json5

This commit is contained in:
2025-03-26 00:55:03 +08:00
parent 501a92eb88
commit 7d93f0eef3
15 changed files with 356 additions and 426 deletions

13
src/modules/config.ts Normal file
View File

@@ -0,0 +1,13 @@
import path from 'path';
import dotenv from 'dotenv';
const envFiles = [
path.resolve(process.cwd(), '.env.dev'),
path.resolve(process.cwd(), '.env'),
];
dotenv.config({
path: envFiles,
});
export const config = process.env;
export const port = config.PORT || 4005;

View File

@@ -1,11 +1,5 @@
import { useConfig } from '@kevisual/use-config';
type MinioConfig = {
domain: string;
};
const config = useConfig<MinioConfig>();
/**
* 用来放cookie的域名
*/
export const domain = config.domain || ''; // 请在这里填写你的域名
export const domain = process.env.DOMAIN || ''; // 请在这里填写你的域名

View File

@@ -1,14 +1,15 @@
import { Client, ClientOptions } from 'minio';
import { useConfig } from '@kevisual/use-config';
type MinioConfig = {
minio: ClientOptions & { bucketName: string };
const minioConfig = {
endPoint: process.env.MINIO_ENDPOINT || 'localhost',
port: parseInt(process.env.MINIO_PORT || '9000'),
useSSL: process.env.MINIO_USE_SSL === 'true',
accessKey: process.env.MINIO_ACCESS_KEY,
secretKey: process.env.MINIO_SECRET_KEY,
};
const config = useConfig<MinioConfig>();
export const minioClient = new Client(minioConfig);
const { bucketName, ...minioRest } = config.minio;
export const minioClient = new Client(minioRest);
export { bucketName };
export const bucketName = process.env.MINIO_BUCKET_NAME || 'resources';
if (!minioClient) {
throw new Error('Minio client not initialized');
}

View File

@@ -1,9 +1,5 @@
import { Redis } from 'ioredis';
import { useConfig } from '@kevisual/use-config';
const config = useConfig<{
redis: ConstructorParameters<typeof Redis>;
}>();
// 配置 Redis 连接
export const redis = new Redis({
host: 'localhost', // Redis 服务器的主机名或 IP 地址
@@ -16,7 +12,6 @@ export const redis = new Redis({
return Math.min(times * 50, 2000); // 每次重试时延迟增加
},
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
...config.redis,
});
// 监听连接事件
@@ -31,4 +26,3 @@ redis.on('error', (err) => {
// 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息
export const redisSubscriber = new Redis(); // 用于订阅消息

View File

@@ -1,11 +1,7 @@
import { useConfig } from '@kevisual/use-config';
import childProcess from 'child_process';
const config = useConfig<{
appName: string;
}>();
export const selfRestart = async () => {
const appName = config.appName || 'codecenter';
const appName = 'codecenter';
// 检测 pm2 是否安装和是否有 appName 这个应用
try {
const res = childProcess.execSync(`pm2 list`);

View File

@@ -1,7 +1,6 @@
import { useConfig } from '@kevisual/use-config';
import { Sequelize } from 'sequelize';
type PostgresConfig = {
import { config } from './config.ts';
export type PostgresConfig = {
postgres: {
username: string;
password: string;
@@ -10,14 +9,17 @@ type PostgresConfig = {
database: string;
};
};
const config = useConfig<PostgresConfig>();
const postgresConfig = config.postgres;
if (!postgresConfig) {
console.error('postgres config is required');
if (!config.POSTGRES_PASSWORD || !config.POSTGRES_USER) {
console.error('postgres config is required password and user');
process.exit(1);
}
const postgresConfig = {
username: config.POSTGRES_USER,
password: config.POSTGRES_PASSWORD,
host: config.POSTGRES_HOST || 'localhost',
port: parseInt(config.POSTGRES_PORT || '5432'),
database: config.POSTGRES_DB || 'postgres',
};
// connect to db
export const sequelize = new Sequelize({
dialect: 'postgres',