init base app modules

This commit is contained in:
2025-07-01 18:53:28 +08:00
commit d96c342d3e
23 changed files with 3311 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
// 使用DashScope API进行TTS (文本转语音) 请求
export const dashscopeTTS = async ({ text, voice = 'Chelsie', token }) => {
try {
const response = await fetch('https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
// model: 'qwen-tts',
model: 'qwen-tts-latest',
input: {
text,
voice,
},
}),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('TTS 请求失败:', error);
throw error;
}
};

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

@@ -0,0 +1,4 @@
import { useConfig } from '@kevisual/use-config';
export const config = useConfig();
export const isDev = config.ENV === 'development';

6
src/modules/logger.ts Normal file
View File

@@ -0,0 +1,6 @@
import { Logger } from '@kevisual/logger';
import { config } from './config.ts';
export const logger = new Logger({
level: config.LOG_LEVEL || 'info',
showTime: true,
});

41
src/modules/minio.ts Normal file
View File

@@ -0,0 +1,41 @@
import { Client, ClientOptions } from 'minio';
import { config } from './config.ts';
import { OssBase } from '@kevisual/oss/services';
import { useContextKey } from '@kevisual/context';
const minioConfig = {
endPoint: config.MINIO_ENDPOINT || 'localhost',
port: parseInt(config.MINIO_PORT || '9000'),
useSSL: config.MINIO_USE_SSL === 'true',
accessKey: config.MINIO_ACCESS_KEY,
secretKey: config.MINIO_SECRET_KEY,
};
export const minioClient = useContextKey('minioClient', () => {
return new Client(minioConfig);
});
export const bucketName = config.MINIO_BUCKET_NAME || 'resources';
if (!minioClient) {
throw new Error('Minio client not initialized');
}
export const check = () => {
// 验证权限
(async () => {
const bucketExists = await minioClient.bucketExists(bucketName);
if (!bucketExists) {
await minioClient.makeBucket(bucketName);
}
console.log('bucketExists', bucketExists);
// const res = await minioClient.putObject(bucketName, 'root/test/0.0.1/a.txt', 'test');
// console.log('minio putObject', res);
})();
};
export const oss = useContextKey(
'oss',
new OssBase({
client: minioClient,
bucketName: bucketName,
prefix: '',
}),
);

3
src/modules/notify.ts Normal file
View File

@@ -0,0 +1,3 @@
export const notify = () => {
//
};

38
src/modules/redis.ts Normal file
View File

@@ -0,0 +1,38 @@
import { Redis } from 'ioredis';
import { config } from './config.ts';
import { useContextKey } from '@kevisual/context';
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,
});
redisClient.on('connect', () => {
console.log('Redis client connected successfully');
});
redisClient.on('error', (err) => {
console.error('Redis client error:', err);
});
return redisClient;
};
// 配置 Redis 连接
export const redis = useContextKey('redis', () => createRedisClient());
// 初始化 Redis 客户端
// export const redisPublisher = createRedisClient(); // 用于发布消息
// export const redisSubscriber = createRedisClient(); // 用于订阅消息

6
src/modules/router.ts Normal file
View File

@@ -0,0 +1,6 @@
import { App } from '@kevisual/router';
import { useContextKey } from '@kevisual/context';
const init = () => {
return new App();
};
export const app = useContextKey('app', init);

34
src/modules/sequelize.ts Normal file
View File

@@ -0,0 +1,34 @@
import { Sequelize } from 'sequelize';
import { config } from './config.ts';
import { useContextKey } from '@kevisual/context';
export type PostgresConfig = {
postgres: {
username: string;
password: string;
host: string;
port: number;
database: string;
};
};
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',
};
export const init = async () => {
return new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});
};
export const sequelize = useContextKey('sequelize', () => init());

9
src/modules/user.ts Normal file
View File

@@ -0,0 +1,9 @@
import { sequelize, User, UserInit, Org, OrgInit } from '@kevisual/code-center-module';
export { sequelize, User, UserInit, Org, OrgInit };
export const init = () => {
UserInit();
OrgInit();
};
init();

26
src/utils/random.ts Normal file
View File

@@ -0,0 +1,26 @@
import { customAlphabet } from 'nanoid';
export const letter = 'abcdefghijklmnopqrstuvwxyz';
export const number = '0123456789';
const alphanumeric = `${letter}${number}`;
export const alphanumericWithDash = `${alphanumeric}-`;
export const uuid = customAlphabet(letter);
export const nanoid = customAlphabet(alphanumeric, 10);
export const nanoidWithDash = customAlphabet(alphanumericWithDash, 10);
/**
* 创建一个随机的 id以字母开头的字符串
* @param number
* @returns
*/
export const randomId = (number: number) => {
const _letter = uuid(1);
return `${_letter}${nanoid(number)}`;
};
export const randomLetter = (number: number = 8, opts?: { before?: string; after?: string }) => {
const { before = '', after = '' } = opts || {};
return `${before}${uuid(number)}${after}`;
};