快速参考¶
Shortener 项目的常用命令快速参考。
📦 安装¶
# 从源码构建
cargo build --release
# 安装 CLI
cargo install --path shortener-cli
# Docker
docker compose up -d
🚀 运行¶
💻 本地调试(前后端一体)¶
# 一键启动:自动准备开发密钥 → 等待后端探活 → 启动前端 Vite dev server
just dev
# 行为说明(对齐 acmecast 开发流程):
# - 未导出 JWT_SECRET / ADMIN__PASSWORD_HASH 时自动生成开发默认值
# (管理员 admin / 密码 dev;也可自行 export 覆盖)
# - 后端 :8080 探活通过后才启动 Vite;后端启动失败则终止并输出日志
# - Vite 端口取 VITE_DEV_PORT(缺省 8000),strictPort 开启:
# 端口被占用时显式报错退出,不静默漂移
# - /api 代理到 http://127.0.0.1:8080(务必写 127.0.0.1 而非 localhost,
# 避免 Node ≥17 将 localhost 解析为 IPv6 ::1 导致代理失败)
# 访问
# 页面(Vite 热更新):http://localhost:8000/
# 后端 API 直连: http://localhost:8080/go/
# 停止调试环境(二选一):
# 1. 运行 just dev 的终端按 Ctrl-C(trap 自动回收后端进程组)
# 2. 另开终端执行(后端残留/终端被强杀时适用):
just dev-stop
📚 文档¶
🔨 开发¶
# 格式化代码
cargo fmt
# 代码检查
cargo clippy
# 运行测试
cargo test
# 监视更改
cargo watch -x 'run -p shortener-server'
🐳 Docker¶
# 构建统一镜像(前端 + 后端)
just docker-build
# 运行(SQLite + Redis,可选启用 postgres/mysql profile)
just docker-run
# 查看日志
just docker-logs
# 停止
just docker-stop
🧪 测试¶
📖 CLI 命令¶
# 初始化
shortener-cli init
# 创建短链接
shortener-cli create https://example.com
# 获取详情
shortener-cli get <code>
# 列出所有
shortener-cli list --all
# 更新
shortener-cli update <code> --ourl <new-url>
# 删除
shortener-cli delete <code>
🔧 配置¶
# 服务器配置(config.toml 可选;不提供时纯环境变量启动,env 优先于文件)
vim config.toml
# 纯环境变量最小配置
export SERVER__API_KEY="$(openssl rand -base64 32)"
export ADMIN__PASSWORD_HASH='$argon2id$...' # shortener-server hash-password --password "..."
export DATABASE__URL="sqlite://data/shortener.db?mode=rwc"
export JWT_SECRET="$(openssl rand -base64 48)"
# CLI 配置
shortener-cli init
vim ~/.config/shortener/config.toml
🌐 API¶
# 登录
curl -X POST http://localhost:8080/api/account/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"password"}'
# 创建短链接
curl -X POST http://localhost:8080/api/shortens \
-H "X-API-KEY: your-api-key" \
-H "Content-Type: application/json" \
-d '{"original_url":"https://example.com"}'
# 获取短链接
curl http://localhost:8080/api/shortens/<code> \
-H "X-API-KEY: your-api-key"
# 刷新缓存(清空前缀旧键并从数据库重载)
curl -X POST http://localhost:8080/api/cache/refresh \
-H "X-API-KEY: your-api-key"
🔐 安全¶
📊 性能¶
🛠️ 部署¶
# Systemd
cd deploy/systemd
sudo ./install.sh
sudo systemctl start shortener-server
# Docker
docker compose -f docker/docker-compose.yml up -d
# 交叉编译
./scripts/build-cross.sh --all
📝 贡献¶
# Fork 并克隆
git clone https://github.com/你的用户名/shortener.git
# 创建分支
git checkout -b feature/your-feature
# 提交更改
git commit -m "添加:your feature"
# 推送
git push origin feature/your-feature
🔗 链接¶
📋 检查清单¶
开发前¶
- 安装 Rust 1.90+
- 克隆仓库
- 安装开发工具
- 阅读贡献指南
提交前¶
- 运行
cargo fmt - 运行
cargo clippy - 运行
cargo test - 更新文档
- 编写提交信息
部署前¶
- 运行所有测试
- 更新版本号
- 构建发布版本
- 测试部署
提示: 使用 just --list 或 make help 查看所有可用命令。