Compare commits

...

6 Commits

Author SHA1 Message Date
6bedf9a0ee 更新 tsconfig.json 2025-10-26 02:53:11 +08:00
5cce86d476 fix 2025-05-23 12:05:53 +08:00
5e03ef2dd9 temp 2025-04-30 19:27:31 +08:00
d4aa63eea2 temp
All checks were successful
Sync to CNB / sync (push) Successful in 3s
2025-04-30 19:01:43 +08:00
f28918d724 fix
All checks were successful
Sync to CNB / sync (push) Successful in 4s
2025-04-28 15:15:41 +08:00
e7eb8742f1 fix 2025-04-28 15:15:40 +08:00
10 changed files with 91 additions and 214 deletions

View File

@@ -1,22 +0,0 @@
name: Sync to CNB
on: [push]
# This workflow is triggered on push events to the repository.
jobs:
sync:
runs-on: ubuntu-latest
steps:
- users: actions/checkout@v4
with:
fetch-depth: 0
- name: Sync to CNB Repository
run: |
docker run --rm \
-v ${{ github.workspace }}:${{ github.workspace }} \
-w ${{ github.workspace }} \
-e PLUGIN_TARGET_URL="https://cnb.cool/kevisual/router-template.git" \
-e PLUGIN_AUTH_TYPE="https" \
-e PLUGIN_USERNAME="cnb" \
-e PLUGIN_PASSWORD=${{ secrets.GIT_PASSWORD }} \
-e PLUGIN_SYNC_MODE="rebase" \
tencentcom/git-sync

24
bun.config.mjs Normal file
View File

@@ -0,0 +1,24 @@
// @ts-check
import { resolvePath } from '@kevisual/use-config/env';
import { execSync } from 'node:child_process';
const entry = 'src/index.ts';
const naming = 'app';
/**
* @type {import('bun').BuildConfig}
*/
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [resolvePath(entry, { meta: import.meta })],
outdir: resolvePath('./dist', { meta: import.meta }),
naming: {
entry: `${naming}.js`,
},
external: [],
env: 'KEVISUAL_*',
});
// const cmd = `dts -i src/index.ts -o app.d.ts`;
const cmd = `dts -i ${entry} -o ${naming}.d.ts`;
execSync(cmd, { stdio: 'inherit' });

View File

@@ -6,17 +6,15 @@
"basename": "/root/demo-app", "basename": "/root/demo-app",
"app": { "app": {
"key": "demo-app", "key": "demo-app",
"entry": "dist/app.mjs", "entry": "dist/app.js",
"type": "system-app", "type": "system-app",
"files": [ "files": [
"dist" "dist"
] ]
}, },
"scripts": { "scripts": {
"watch": "rollup -c rollup.config.mjs -w", "dev": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/dev.ts ",
"build": "rollup -c rollup.config.mjs", "build": "rimraf dist && bun run bun.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\" ",
"test": "tsx test/**/*.ts", "test": "tsx test/**/*.ts",
"clean": "rm -rf dist", "clean": "rm -rf dist",
"pub": "npm run build && envision pack -p -u", "pub": "npm run build && envision pack -p -u",
@@ -35,48 +33,33 @@
"access": "public" "access": "public"
}, },
"dependencies": { "dependencies": {
"@kevisual/code-center-module": "0.0.18", "@kevisual/code-center-module": "0.0.19",
"@kevisual/use-config": "^1.0.11", "@kevisual/router": "0.0.21",
"@kevisual/mark": "0.0.7", "@kevisual/use-config": "^1.0.17",
"@kevisual/router": "0.0.13",
"cookie": "^1.0.2", "cookie": "^1.0.2",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"formidable": "^3.5.4", "formidable": "^3.5.4",
"lodash-es": "^4.17.21" "lodash-es": "^4.17.21"
}, },
"devDependencies": { "devDependencies": {
"@kevisual/types": "^0.0.7", "@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.11", "@kevisual/use-config": "^1.0.17",
"@rollup/plugin-alias": "^5.1.1", "@types/bun": "^1.2.14",
"@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",
"@types/crypto-js": "^4.2.2", "@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5", "@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^22.15.2", "@types/node": "^22.15.21",
"commander": "^13.1.0", "commander": "^14.0.0",
"concurrently": "^9.1.2", "concurrently": "^9.1.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"inquire": "^0.4.8", "inquire": "^0.4.8",
"ioredis": "^5.6.1", "ioredis": "^5.6.1",
"jsrepo": "^1.47.1",
"nodemon": "^3.1.10", "nodemon": "^3.1.10",
"pg": "^8.15.6", "pg": "^8.16.0",
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"pm2": "^6.0.5",
"rimraf": "^6.0.1", "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", "sequelize": "^6.37.7",
"tape": "^5.9.0", "tape": "^5.9.0",
"tsx": "^4.19.3",
"typescript": "^5.8.3" "typescript": "^5.8.3"
}, },
"packageManager": "pnpm@10.10.0" "packageManager": "pnpm@10.11.0"
} }

View File

@@ -1,75 +0,0 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import pkgs from './package.json' with {type: 'json'};
const isDev = process.env.NODE_ENV === 'development';
const input = isDev ? './src/dev.ts' : './src/main.ts';
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input,
output: {
dir: './dist',
entryFileNames: 'app.mjs',
chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
},
plugins: [
replace({
preventAssignment: true, // 防止意外赋值
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
APP_VERSION: JSON.stringify(pkgs.version),
}),
alias({
// only esbuild needs to be configured
entries: [
{ find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录
{ find: 'http', replacement: 'node:http' },
{ find: 'https', replacement: 'node:https' },
{ find: 'fs', replacement: 'node:fs' },
{ find: 'path', replacement: 'node:path' },
{ find: 'crypto', replacement: 'node:crypto' },
{ find: 'zlib', replacement: 'node:zlib' },
{ find: 'stream', replacement: 'node:stream' },
{ find: 'net', replacement: 'node:net' },
{ find: 'tty', replacement: 'node:tty' },
{ find: 'tls', replacement: 'node:tls' },
{ find: 'buffer', replacement: 'node:buffer' },
{ find: 'timers', replacement: 'node:timers' },
// { find: 'string_decoder', replacement: 'node:string_decoder' },
{ find: 'dns', replacement: 'node:dns' },
{ find: 'domain', replacement: 'node:domain' },
{ find: 'os', replacement: 'node:os' },
{ find: 'events', replacement: 'node:events' },
{ find: 'url', replacement: 'node:url' },
{ find: 'assert', replacement: 'node:assert' },
{ find: 'util', replacement: 'node:util' },
],
}),
resolve({
preferBuiltins: true, // 强制优先使用内置模块
}),
commonjs(),
esbuild({
target: 'node22', //
minify: false, // 启用代码压缩
tsconfig: 'tsconfig.json',
}),
json(),
],
external: [
/@kevisual\/router(\/.*)?/, //, // 路由
/@kevisual\/use-config(\/.*)?/, //
'sequelize', // 数据库 orm
'ioredis', // redis
'pg', // pg
],
};
export default config;

View File

@@ -1,6 +1,5 @@
import { app } from './index.ts'; import { app } from './index.ts';
import { useConfig } from '@kevisual/use-config/env'; import { config } from './modules/config.ts';
app app
.route({ .route({
path: 'auth', path: 'auth',
@@ -38,7 +37,6 @@ app
}) })
.addTo(app); .addTo(app);
const config = useConfig();
const port = config.PORT || 4000; const port = config.PORT || 4000;
console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo'); 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 { 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 连接 // 配置 Redis 连接
export const redis = new Redis({ export const redis = useConfigKey('redis', () => initRedis(config));
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);
});
// 初始化 Redis 客户端 // 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息 export const redisPublisher = new Redis(); // 用于发布消息

View File

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

View File

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