You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
谢亚昕 695da012de feat(profile): 增加头像上传功能并优化联系表单页面 3 months ago
.specstory feat: 更新路由系统并添加错误响应格式化功能 6 months ago
.trae feat: 重构项目结构并添加静态文件服务和路由功能 6 months ago
.vscode feat(profile): 增加头像上传功能并优化联系表单页面 3 months ago
database feat(profile): 增加头像上传功能并优化联系表单页面 3 months ago
docs chore(env): 增强环境变量配置和验证体系 3 months ago
public feat(profile): 增加头像上传功能并优化联系表单页面 3 months ago
scripts chore(env): 增强环境变量配置和验证体系 3 months ago
src feat(profile): 增加头像上传功能并优化联系表单页面 3 months ago
.cursorindexingignore feat: 更新路由系统并添加错误响应格式化功能 6 months ago
.dockerignore 新增 Dockerfile 和 docker-compose.yml 文件,配置多阶段构建和服务管理 3 months ago
.env.example chore(env): 增强环境变量配置和验证体系 3 months ago
.gitignore init 6 months ago
.npmrc init 6 months ago
.prettierrc init 6 months ago
Dockerfile 修复Dockerfile文件 3 months ago
README.md chore(env): 增强环境变量配置和验证体系 3 months ago
bun.lockb 增加缩略图 3 months ago
docker-compose.yml chore(env): 增强环境变量配置和验证体系 3 months ago
entrypoint.sh 重构 Dockerfile,优化构建流程,更新 entrypoint.sh 脚本,修复依赖管理,调整中间件导入路径 3 months ago
jsconfig.json feat: add site configuration management with database migration and seeding 6 months ago
knexfile.mjs init 6 months ago
package.json chore(env): 增强环境变量配置和验证体系 3 months ago
vite.config.ts feat: 添加 Vite 配置文件,设置构建和插件选项 5 months ago

README.md

koa3-demo

当前支持功能:

  • 路由
  • 日志
  • 权限
  • 数据库
  • 缓存
  • 界面
  • 定时任务
  • htmx
  • 环境变量验证

快速开始

1. 环境配置

复制环境变量模板文件:

cp .env.example .env

编辑 .env 文件,设置必需的环境变量:

  • SESSION_SECRET: 会话密钥(至少32个字符)
  • JWT_SECRET: JWT密钥(至少32个字符)

2. 安装依赖

bun install

3. 初始化数据库

bun run dev:init

4. 启动开发服务器

bun run dev

环境变量说明

项目启动时会自动验证环境变量配置,确保应用安全性:

必需环境变量

  • SESSION_SECRET: 会话密钥,支持多密钥轮换(逗号分隔)
  • JWT_SECRET: JWT令牌密钥(最少32字符)

可选环境变量

  • NODE_ENV: 运行环境 (development/production/test)
  • PORT: 服务器端口 (默认: 3000)
  • LOG_DIR: 日志目录 (默认: logs)
  • HTTPS_ENABLE: 是否启用HTTPS (默认: off)

详细配置请参考 .env.example 文件。

数据库查询缓存(QueryBuilder 扩展)

为 Knex QueryBuilder 增加了内存级缓存能力,支持 TTL、自定义 key、只读/只写、按前缀失效与全局工具访问。默认 key 为查询的 toString(),也可通过 cacheAs() 指定。

可用方法:

  • cache(ttlMs?): 读取缓存,不存在则执行 SQL 并写入缓存;可选 TTL(毫秒)。
  • cacheAs(customKey): 为当前查询设置自定义缓存 key(链式,返回 builder)。
  • cacheSet(value, ttlMs?): 手动写入当前查询 key 的缓存,返回写入的值。
  • cacheGet(): 仅从缓存读取当前查询 key 的值(不命中则返回 undefined,不会执行 SQL)。
  • cacheInvalidate(): 使当前查询 key 的缓存失效(删除)。
  • cacheInvalidateByPrefix(prefix): 按前缀批量失效。

全局工具(命名导出 DbQueryCache,便于在查询构建器之外操作缓存):

  • get/set/has/delete/clear/clearByPrefix/stats()

示例:

import db, { DbQueryCache } from "./src/db/index.js"

// 1) 简单缓存 5 秒
const u1 = await db("users").where({ id: 1 }).first().cache(5000)

// 2) 自定义 key + 只读命中
const key = "users:count:active"
const count = await db("users").where({ active: 1 }).count({ c: "*" }).cacheAs(key).cache(10000)
const cachedOnly = db("users").where({ active: 1 }).cacheAs(key).cacheGet() // 命中则返回值,不命中为 undefined

// 3) 手动写入/覆盖缓存 15 秒
db("users").where({ active: 1 }).cacheAs(key).cacheSet([{ c: 123 }], 15000)

// 4) 单点或前缀失效
db("users").where({ active: 1 }).cacheAs(key).cacheInvalidate()
db.queryBuilder().cacheInvalidateByPrefix("users:")

// 5) 全局操作
DbQueryCache.clearByPrefix("users:")
const stats = DbQueryCache.stats() // { size, valid, expired }

注意:

  • 该实现为进程内内存缓存,适合单实例与读多写少场景;多实例下需用外部缓存(如 Redis)替换/扩展。
  • TTL 为可选,未设置则永久有效,直到被失效或进程重启。
  • 自定义 key 建议使用明确的命名空间前缀(如 users:site:),以便使用前缀批量失效。

📚 项目文档

🔧 开发和部署

测试环境变量配置

bun run test:env

生产环境部署

# 使用Docker
docker-compose up -d

# 或直接构建
docker build -t koa3-demo .
docker run -p 3000:3000 koa3-demo