Skip to content

类不蒜子可独立部署的hexo数据统计框架

License

Notifications You must be signed in to change notification settings

JackeyLea/cusuanzi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cusuanzi

类不蒜子可独立部署的hexo数据统计框架

C++版busuanzi

环境

  • cpp >= 17
  • cmake >= 3.16
  • yaml-cpp
  • openssl
  • cpp-redis
  • nlohmann-json
  • spdlog
  • httplib
  • sqlite3 (数据库持久化)

功能特性

核心功能

  • ✅ 站点统计(PV/UV)
  • ✅ 页面统计(PV/UV)
  • ✅ 今日统计
  • ✅ 独立IP统计
  • ✅ Redis 数据存储
  • ✅ SQLite3 数据库持久化

数据存储模式

  • 明文模式 (plain): 使用 IP|User-Agent 作为标识,便于调试和迁移
  • 哈希模式 (hash): 使用 MD5/MD516 哈希作为标识,保护隐私

数据库特性

  • SQLite3 持久化存储
  • 自动 Redis 到 SQLite 同步
  • 可配置的数据保留周期
  • 历史数据查询 API
  • 每日统计汇总

编译

vcpkg install
mkdir build
cmake -S . -B .\build -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=".\build" -DCMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_MANIFEST_DIR="." # 生成S2019工程
cmake --build .\build --config Release # 编译
cmake --build .\build --config Release --target install # 安装

sudo apt-get install build-essential cmake pkg-config libyaml-cpp-dev libssl-dev nlohmann-json3-dev libspdlog-dev libhiredis-dev libsqlite3-dev # Linux安装依赖
docker build -f dockerfile.cpp -t busuanzi-cpp # 编译docker镜像
docker-compose -f docker-compose.cpp.yaml up # 运行docker容器

配置

基础配置 (config.yaml)

Web:
  Address: 0.0.0.0:8080
  Debug: false
  Log: false

Redis:
  Address: 127.0.0.1:6379
  Password: ""
  Database: 0
  Prefix: bsz

Bsz:
  Expire: 0
  Secret: ""      # 生产环境必须设置
  Encrypt: "MD516"
  PathStyle: true
  DataStorage: "plain"  # 数据存储方式

Database:
  Enabled: true
  Path: "busuanzi.db"
  SyncInterval: 300  # 同步间隔(秒)
  RetentionDays: 365  # 保留天数

环境变量覆盖

WEB_ADDRESS=0.0.0.0:8080
REDIS_ADDRESS=localhost:6379
BSZ_SECRET=your-secret-key
BSZ_DATA_STORAGE=plain
DB_ENABLED=true
DB_PATH=busuanzi.db
DB_SYNC_INTERVAL=300

API 端点

基础统计

  • POST /api - 统计并返回数据
  • GET /api - 获取统计数据
  • PUT /api - 异步提交数据

完整统计

  • GET /api/full - 获取完整统计(包括今日统计、IP统计)
  • GET /api/today - 获取今日统计
  • GET /api/ips - 获取IP统计
  • GET /api/ip-records - 获取IP访问记录

历史数据查询

  • GET /api/history/stats?start_date=2025-01-01&end_date=2025-01-31 - 查询历史统计
  • GET /api/history/daily?limit=30 - 查询每日统计汇总
  • GET /api/history/ip-records?date=2025-01-01&limit=100 - 查询IP记录

其他

  • GET /jsonp - JSONP 兼容
  • GET /ping - 健康检查

使用示例

基础使用

fetch('http://localhost:8080/api', {
  method: 'POST',
  headers: { 'x-bsz-referer': 'https://yourblog.com/post-1' }
})
.then(response => response.json())
.then(data => {
  console.log('站点PV:', data.data.site_pv);
  console.log('站点UV:', data.data.site_uv);
  console.log('今日PV:', data.data.site_today_pv);
});

历史数据查询

// 查询最近30天数据
fetch('http://localhost:8080/api/history/stats')
  .then(r => r.json())
  .then(data => console.log(data.data));

// 查询指定日期范围
fetch('http://localhost:8080/api/history/stats?start_date=2025-01-01&end_date=2025-01-31')
  .then(r => r.json())
  .then(data => console.log(data.data));

// 查询每日统计
fetch('http://localhost:8080/api/history/daily?limit=30')
  .then(r => r.json())
  .then(data => console.log(data.data));

安全建议

  1. 生产环境必须设置 Secret

    Bsz:
      Secret: "your-strong-random-secret-here"
  2. 启用 Redis 认证

    Redis:
      Password: "your-redis-password"
      TLS: true
  3. 数据库安全

    • 限制数据库文件权限
    • 定期备份数据
    • 设置合理的保留周期

文档

  • README_CPP.md - C++ 版本详细文档
  • PROJECT_SUMMARY.md - 项目概述
  • MIGRATION_GUIDE.md - 迁移指南
  • DATA_STORAGE_MODE.md - 数据存储模式说明
  • USAGE_EXAMPLES.md - 使用示例
  • DEPLOYMENT_GUIDE.md - 部署指南
  • CODE_REVIEW.md - 代码审查报告
  • NEW_FEATURES_SUMMARY.md - 新功能总结

代码质量

  • ✅ 参数化查询,防止 SQL 注入
  • ✅ 输入验证,防止无效参数
  • ✅ 空指针检查,提高稳定性
  • ✅ 详细的日志记录
  • ✅ 线程安全设计
  • ✅ 错误处理完善

许可证

MIT License

About

类不蒜子可独立部署的hexo数据统计框架

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published