Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

快速开始

环境要求

  • Rust stable,支持 Rust 2024 Edition
  • Docker 和 Docker Compose

初始化环境变量并安装锁定版本的开发依赖:

make init
make install

启动服务

make db
make db-migrate
make dev

应用在宿主机运行,PostgreSQL 在 Compose 中运行。默认地址是 127.0.0.1:3000,默认数据库连接为 postgres://rustloom:rustloom@127.0.0.1:5432/rustloom。

make init 只在 .env 不存在时从 .env.example 创建文件,不覆盖已有配置。 make db 启动并等待当前数据服务健康;默认服务列表为 postgres,以后新增 Redis 等 Compose 数据服务时同步扩展 DATA_SERVICES。make db-migrate 会先确保数据 服务已启动,再执行 forward-only migration。make dev 包含前述数据库准备步骤, 并以 Rust dev profile 启动应用。

验证服务:

curl -i http://127.0.0.1:3000/health
curl -i http://127.0.0.1:3000/ready

注册并读取 Profile:

curl -i -c .cookies \
  -H 'content-type: application/json' \
  -d '{"email":"user@example.com","password":"correct horse 电池 staple","display_name":"Shawn"}' \
  http://127.0.0.1:3000/api/v1/auth/register
curl -i -b .cookies http://127.0.0.1:3000/api/v1/profile

停止数据库但保留数据:

docker compose stop postgres

删除本地数据库数据是破坏性操作,只能在明确需要重建开发环境时执行:

docker compose down --volumes

环境变量

变量必填默认值用途
DATABASE_URL是无PostgreSQL 连接字符串
DATABASE_MAX_CONNECTIONS否10连接池最大连接数
DATABASE_ACQUIRE_TIMEOUT_SECONDS否2获取数据库连接的最大等待秒数
SESSION_COOKIE_SECURE否false是否给 Session Cookie 设置 Secure;生产必须为 true
SERVER_ADDRESS否127.0.0.1:3000HTTP 监听地址

应用启动时会读取仓库根目录的 .env。.env 不得提交,新增变量时必须同步 更新 .env.example 和本页。