Compare commits
6 Commits
fd776e8176
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bedf9a0ee | |||
| 5cce86d476 | |||
| 5e03ef2dd9 | |||
| d4aa63eea2 | |||
| f28918d724 | |||
| e7eb8742f1 |
22
.github/workflows/git-sync.yml
vendored
22
.github/workflows/git-sync.yml
vendored
@@ -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
24
bun.config.mjs
Normal 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' });
|
||||
43
package.json
43
package.json
@@ -6,17 +6,15 @@
|
||||
"basename": "/root/demo-app",
|
||||
"app": {
|
||||
"key": "demo-app",
|
||||
"entry": "dist/app.mjs",
|
||||
"entry": "dist/app.js",
|
||||
"type": "system-app",
|
||||
"files": [
|
||||
"dist"
|
||||
]
|
||||
},
|
||||
"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",
|
||||
@@ -35,48 +33,33 @@
|
||||
"access": "public"
|
||||
},
|
||||
"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/code-center-module": "0.0.19",
|
||||
"@kevisual/router": "0.0.21",
|
||||
"@kevisual/use-config": "^1.0.17",
|
||||
"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.10",
|
||||
"@kevisual/use-config": "^1.0.17",
|
||||
"@types/bun": "^1.2.14",
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/formidable": "^3.4.5",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
"@types/node": "^22.15.2",
|
||||
"commander": "^13.1.0",
|
||||
"@types/node": "^22.15.21",
|
||||
"commander": "^14.0.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",
|
||||
"pg": "^8.16.0",
|
||||
"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"
|
||||
"packageManager": "pnpm@10.11.0"
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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');
|
||||
|
||||
@@ -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
3
src/modules/config.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { useConfig } from '@kevisual/use-config/env';
|
||||
|
||||
export const config = useConfig();
|
||||
@@ -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(); // 用于发布消息
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -1,25 +1,10 @@
|
||||
{
|
||||
"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"
|
||||
"node_modules/@types"
|
||||
],
|
||||
"declaration": true,
|
||||
"noEmit": false,
|
||||
"allowImportingTsExtensions": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"esModuleInterop": true,
|
||||
"paths": {
|
||||
"@/*": [
|
||||
"src/*"
|
||||
|
||||
Reference in New Issue
Block a user