This commit is contained in:
熊潇 2025-04-30 19:28:04 +08:00
parent 3f3f08cc4e
commit 26ca4c21c8
11 changed files with 194 additions and 3776 deletions

View File

@ -3,3 +3,6 @@ POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=postgres
REDIS_HOST=localhost
REDIS_PORT=6379

View File

@ -1,5 +1,11 @@
name: Sync to CNB
on: [push]
on:
push:
branches:
# - main
- 'releases/**'
workflow_dispatch:
# This workflow is triggered on push events to the repository.
jobs:
sync:

20
bun.config.mjs Normal file
View File

@ -0,0 +1,20 @@
// @ts-check
// https://bun.sh/docs/bundler
// @ts-ignore
import pkg from './package.json';
import { resolvePath as rp } from '@kevisual/use-config/env';
// bun run src/index.ts --
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [rp('./src/main.ts')],
outdir: rp('./dist'),
naming: {
entry: 'app.mjs',
},
define: {
VERSION: JSON.stringify(pkg.version),
},
env: 'KEVISUAL_*',
});

View File

@ -13,10 +13,8 @@
]
},
"scripts": {
"watch": "rollup -c rollup.config.mjs -w",
"build": "rollup -c rollup.config.mjs",
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.mjs",
"dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
"dev": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/dev.ts ",
"build": "rimraf dist && bun run bun.config.mjs",
"test": "tsx test/**/*.ts",
"clean": "rm -rf dist",
"pub": "npm run build && envision pack -p -u",
@ -36,46 +34,31 @@
},
"dependencies": {
"@kevisual/code-center-module": "0.0.18",
"@kevisual/use-config": "^1.0.11",
"@kevisual/mark": "0.0.7",
"@kevisual/router": "0.0.13",
"@kevisual/use-config": "^1.0.12",
"cookie": "^1.0.2",
"dayjs": "^1.11.13",
"formidable": "^3.5.4",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@kevisual/types": "^0.0.7",
"@kevisual/use-config": "^1.0.11",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-typescript": "^12.1.2",
"@kevisual/types": "^0.0.9",
"@kevisual/use-config": "^1.0.12",
"@types/bun": "^1.2.11",
"@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.15.2",
"@types/node": "^22.15.3",
"commander": "^13.1.0",
"concurrently": "^9.1.2",
"cross-env": "^7.0.3",
"inquire": "^0.4.8",
"ioredis": "^5.6.1",
"jsrepo": "^1.47.1",
"nodemon": "^3.1.10",
"pg": "^8.15.6",
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"pm2": "^6.0.5",
"rimraf": "^6.0.1",
"rollup": "^4.40.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^6.2.1",
"sequelize": "^6.37.7",
"tape": "^5.9.0",
"tsx": "^4.19.3",
"typescript": "^5.8.3"
},
"packageManager": "pnpm@10.10.0"

3762
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,5 @@
import { app } from './index.ts';
import { useConfig } from '@kevisual/use-config/env';
import { config } from './modules/config.ts';
app
.route({
path: 'auth',
@ -38,7 +37,6 @@ app
})
.addTo(app);
const config = useConfig();
const port = config.PORT || 4000;
console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo');

View File

@ -1,37 +0,0 @@
import { pino } from 'pino';
import { useConfig } from '@kevisual/use-config/env';
const config = useConfig();
export const logger = pino({
level: config.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
},
},
serializers: {
error: pino.stdSerializers.err,
req: pino.stdSerializers.req,
res: pino.stdSerializers.res,
},
base: {
app: 'ai-chat',
env: process.env.NODE_ENV || 'development',
},
});
export const logError = (message: string, data?: any) => logger.error({ data }, message);
export const logWarning = (message: string, data?: any) => logger.warn({ data }, message);
export const logInfo = (message: string, data?: any) => logger.info({ data }, message);
export const logDebug = (message: string, data?: any) => logger.debug({ data }, message);
export const log = {
error: logError,
warn: logWarning,
info: logInfo,
debug: logDebug,
};

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

@ -0,0 +1,3 @@
import { useConfig } from '@kevisual/use-config/env';
export const config = useConfig();

View File

@ -1,27 +1,42 @@
import { Redis } from 'ioredis';
import { config } from './config.ts';
type initRedisOpts = {
onConnect?: () => void; // 连接成功的回调函数
onError?: (msg?: any) => void; // 连接断开的回调函数
};
const initRedis = (config?: any, options?: initRedisOpts) => {
const redis = new Redis({
host: config?.REDIS_HOST || 'localhost', // Redis 服务器的主机名或 IP 地址
port: config?.REDIS_PORT || 6379, // Redis 服务器的端口号
// password: 'your_password', // Redis 的密码 (如果有)
db: 0, // 要使用的 Redis 数据库索引 (0-15)
keyPrefix: '', // key 前缀
retryStrategy(times) {
// 连接重试策略
return Math.min(times * 50, 2000); // 每次重试时延迟增加
},
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
...config, // 其他配置项
});
// 监听连接事件
redis.on('connect', () => {
if (options?.onConnect) {
options.onConnect();
console.log('Redis 连接成功');
}
});
redis.on('error', (err) => {
console.error('Redis 连接错误', err);
if (options?.onError) {
options.onError(err);
}
});
return redis;
};
// 配置 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, // 允许请求重试的次数 (如果需要无限次重试)
});
// 监听连接事件
redis.on('connect', () => {
console.log('Redis 连接成功');
});
redis.on('error', (err) => {
console.error('Redis 连接错误', err);
});
export const redis = useConfigKey('redis', () => initRedis(config));
// 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息

View File

@ -1,7 +1,6 @@
import { Sequelize } from 'sequelize';
import { useConfig } from '@kevisual/use-config/env';
const config = useConfig();
import { config } from './config.ts';
import { useContextKey } from '@kevisual/use-config/context';
export type PostgresConfig = {
postgres: {
@ -23,9 +22,13 @@ const postgresConfig = {
port: parseInt(config.POSTGRES_PORT || '5432'),
database: config.POSTGRES_DB || 'postgres',
};
// connect to db
export const sequelize = new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});
export const init = async () => {
return new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});
};
export const sequelize = useContextKey('sequelize', () => init());

View File

@ -1,25 +1,7 @@
{
"extends": "@kevisual/types/json/backend.json",
"compilerOptions": {
"module": "nodenext",
"target": "esnext",
"noImplicitAny": false,
"outDir": "./dist",
"sourceMap": false,
"allowJs": true,
"newLine": "LF",
"baseUrl": "./",
"typeRoots": [
"node_modules/@types",
"node_modules/@kevisual/types"
],
"declaration": true,
"noEmit": false,
"allowImportingTsExtensions": true,
"emitDeclarationOnly": true,
"moduleResolution": "NodeNext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"paths": {
"@/*": [
"src/*"