perf router template

This commit is contained in:
abearxiong 2025-04-07 14:36:52 +08:00
parent 117be3062d
commit 6807388ab5
18 changed files with 996 additions and 464 deletions

5
.env.example Normal file
View File

@ -0,0 +1,5 @@
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=postgres

3
.gitignore vendored
View File

@ -12,3 +12,6 @@ cache-file
/apps
logs
.env*
!.env.example

1
README.md Normal file
View File

@ -0,0 +1 @@
# router app template

12
cmd/index.ts Normal file
View File

@ -0,0 +1,12 @@
import { program } from './program.ts';
import { Command } from 'commander';
program.version('1.0.0');
program.addCommand(
new Command('test').action(() => {
console.log('test');
}),
);
program.parse(process.argv);

3
cmd/program.ts Normal file
View File

@ -0,0 +1,3 @@
import { program } from 'commander';
export { program };

View File

@ -1,72 +1,82 @@
{
"name": "demo-app",
"version": "0.0.1",
"description": "",
"main": "index.js",
"app": {
"key": "demo-app",
"entry": "dist/app.mjs",
"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",
"test": "tsx test/**/*.ts",
"dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
"clean": "rm -rf dist",
"prepub": "envision switch root",
"pub": "npm run build && envision pack -p -u"
},
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>",
"license": "MIT",
"type": "module",
"types": "types/index.d.ts",
"files": [
"types",
"dist",
"src"
],
"dependencies": {
"@kevisual/code-center-module": "0.0.18",
"@kevisual/mark": "0.0.7",
"@kevisual/router": "0.0.10",
"cookie": "^1.0.2",
"dayjs": "^1.11.13",
"formidable": "^3.5.2",
"json5": "^2.2.3",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@kevisual/types": "^0.0.6",
"@kevisual/use-config": "^1.0.10",
"@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",
"@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.14.0",
"concurrently": "^9.1.2",
"cross-env": "^7.0.3",
"jsrepo": "^1.45.3",
"nodemon": "^3.1.9",
"pm2": "^6.0.5",
"rimraf": "^6.0.1",
"rollup": "^4.39.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^6.2.1",
"tape": "^5.9.0",
"tsx": "^4.19.3",
"typescript": "^5.8.2"
},
"packageManager": "pnpm@10.7.1"
"name": "demo-app",
"version": "0.0.1",
"description": "",
"main": "index.js",
"basename": "/root/demo-app",
"app": {
"key": "demo-app",
"entry": "dist/app.mjs",
"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\" ",
"test": "tsx test/**/*.ts",
"clean": "rm -rf dist",
"pub": "npm run build && envision pack -p -u",
"cmd": "tsx cmd/index.ts "
},
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>",
"license": "MIT",
"type": "module",
"types": "types/index.d.ts",
"files": [
"dist",
"src"
],
"publishConfig": {
"access": "public"
},
"dependencies": {
"@kevisual/code-center-module": "0.0.18",
"@kevisual/use-config": "^1.0.10",
"@kevisual/mark": "0.0.7",
"@kevisual/router": "0.0.10",
"cookie": "^1.0.2",
"dayjs": "^1.11.13",
"formidable": "^3.5.2",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@kevisual/types": "^0.0.6",
"@kevisual/use-config": "^1.0.10",
"@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",
"@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.14.0",
"commander": "^13.1.0",
"concurrently": "^9.1.2",
"cross-env": "^7.0.3",
"inquire": "^0.4.8",
"ioredis": "^5.6.0",
"jsrepo": "^1.45.3",
"nodemon": "^3.1.9",
"pg": "^8.14.1",
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"pm2": "^6.0.5",
"rimraf": "^6.0.1",
"rollup": "^4.39.0",
"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.7.1"
}

906
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@ 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/index.ts';
const input = isDev ? './src/dev.ts' : './src/main.ts';
/**
* @type {import('rollup').RollupOptions}
*/
@ -24,7 +24,7 @@ const config = {
replace({
preventAssignment: true, // 防止意外赋值
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
VERSION: JSON.stringify(pkgs.version),
APP_VERSION: JSON.stringify(pkgs.version),
}),
alias({
// only esbuild needs to be configured
@ -67,9 +67,9 @@ const config = {
/@kevisual\/router(\/.*)?/, //, // 路由
/@kevisual\/use-config(\/.*)?/, //
// 'sequelize', // 数据库 orm
// 'ioredis', // redis
// 'pg', // pg
'sequelize', // 数据库 orm
'ioredis', // redis
'pg', // pg
],
};
export default config;

View File

@ -1,16 +0,0 @@
import { app } from './app.ts';
import { useConfig } from '@kevisual/use-config';
app
.route({
path: 'demo',
key: 'demo',
})
.define(async (ctx) => {
ctx.body = '123';
})
.addTo(app);
const config = useConfig();
console.log('run demo: http://localhost:' + config.port + '/api/router?path=demo&key=demo');

View File

@ -1,8 +1,48 @@
import { useConfig } from '@kevisual/use-config';
import { app } from './index.ts';
import { useConfig } from '@kevisual/use-config/env';
app
.route({
path: 'auth',
id: 'auth',
})
.define(async (ctx) => {
ctx.query.token = '123';
ctx.state.tokenUser = {
id: '123',
username: 'admin',
};
})
.addTo(app);
app
.route({
path: 'auth-admin',
id: 'auth-admin',
})
.define(async (ctx) => {
ctx.body = '123';
ctx.state.tokenUser = {
id: '123',
username: 'admin',
};
})
.addTo(app);
app
.route({
path: 'demo',
key: 'demo',
})
.define(async (ctx) => {
ctx.body = '123';
})
.addTo(app);
const config = useConfig();
const port = config.PORT || 4000;
app.listen(config.port, () => {
console.log(`server is running at http://localhost:${config.port}`);
console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo');
app.listen(port, () => {
console.log(`server is running at http://localhost:${port}`);
});

View File

@ -1,4 +1,3 @@
import { app } from './app.ts';
import './demo-route.ts';
export { app };

37
src/logger/index.ts Normal file
View File

@ -0,0 +1,37 @@
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,
};

View File

@ -1,10 +1,9 @@
// 单应用实例启动
import { useConfig } from '@kevisual/use-config/env';
import { app } from './index.ts';
const config = useConfig();
app.listen(config.port, () => {
console.log(`server is running at http://localhost:${config.port}`);
const port = config.PORT || 4000;
app.listen(port, () => {
console.log(`server is running at http://localhost:${port}`);
});

View File

@ -1,9 +0,0 @@
import { Mark, markModelInit } from '@kevisual/mark';
export { Mark, markModelInit };
export const init = () => {
markModelInit({
tableName: '',
});
};

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

@ -0,0 +1,28 @@
import { Redis } from 'ioredis';
// 配置 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);
});
// 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息
export const redisSubscriber = new Redis(); // 用于订阅消息

View File

@ -1 +1,31 @@
export { sequelize, redis } from '@kevisual/code-center-module';
import { Sequelize } from 'sequelize';
import { useConfig } from '@kevisual/use-config/env';
const config = useConfig();
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',
};
// connect to db
export const sequelize = new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});

View File

@ -0,0 +1,119 @@
import { Op } from 'sequelize';
import { AppDemoModel } from './models/index.ts';
import { app } from '@/app.ts';
app
.route({
path: 'app-demo',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query;
const searchWhere = search
? {
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { summary: { [Op.like]: `%${search}%` } }],
}
: {};
const { rows: appDemo, count } = await AppDemoModel.findAndCountAll({
where: {
uid: tokenUser.uid,
...searchWhere,
},
offset: (page - 1) * pageSize,
limit: pageSize,
order: [['updatedAt', sort]],
});
ctx.body = {
list: appDemo,
pagination: {
page,
current: page,
pageSize,
total: count,
},
};
})
.addTo(app);
app
.route({
path: 'app-demo',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, data, updatedAt: _clear, createdAt: _clear2, ...rest } = ctx.query.data;
let appDemo: AppDemoModel;
let isNew = false;
if (id) {
const appDemo = await AppDemoModel.findByPk(id);
if (appDemo.uid !== tokenUser.uid) {
ctx.throw(403, 'No permission');
}
} else {
appDemo = await AppDemoModel.create({
data: data,
...rest,
uid: tokenUser.uid,
});
isNew = true;
}
if (!appDemo) {
ctx.throw(404, 'AppDemo not found');
}
if (!isNew) {
appDemo = await appDemo.update({
data: { ...appDemo.data, ...data },
...rest,
});
}
ctx.body = appDemo;
})
.addTo(app);
app
.route({
path: 'app-demo',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, force = false } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id is required');
}
const appDemo = await AppDemoModel.findByPk(id);
if (appDemo.uid !== tokenUser.uid) {
ctx.throw(403, 'No permission');
}
await appDemo.destroy({ force });
ctx.body = appDemo;
})
.addTo(app);
app
.route({
path: 'app-demo',
key: 'get',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id is required');
}
const appDemo = await AppDemoModel.findByPk(id);
if (appDemo.uid !== tokenUser.uid) {
ctx.throw(403, 'No permission');
}
ctx.body = appDemo;
})
.addTo(app);

View File

@ -0,0 +1,71 @@
import { sequelize } from '@/modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export interface AppDemoData {
[key: string]: any;
}
export type AppDemo = Partial<InstanceType<typeof AppDemoModel>>;
export class AppDemoModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare summary: string;
declare data: AppDemoData;
declare tags: string[];
declare version: string;
declare uid: string;
declare createdAt: Date;
declare updatedAt: Date;
}
AppDemoModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
title: {
type: DataTypes.TEXT,
defaultValue: '',
},
description: {
type: DataTypes.TEXT,
defaultValue: '',
},
summary: {
type: DataTypes.TEXT,
defaultValue: '',
},
tags: {
type: DataTypes.JSONB,
defaultValue: [],
},
version: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
data: {
type: DataTypes.JSONB,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: false,
},
},
{
sequelize,
tableName: 'kv_app_demo',
paranoid: true,
},
);
AppDemoModel.sync({ alter: true, logging: false }).catch((e) => {
console.error('AppDemoModel sync', e);
});