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

应用架构

RustLoom 保持单个 Cargo package 和单体二进制,不建立 workspace 或微服务。

模块职责

  • main:加载环境、建立数据库连接、启动 Session 清理任务、绑定端口并服务。
  • config:解析和校验环境变量,不包含业务逻辑。
  • database:创建 PostgreSQL 连接池。
  • app:组合 Router、共享状态和 Session middleware。
  • identity:输入校验、身份 handler、认证提取器、用户查询和密码服务。
  • error:把内部错误转换为稳定的对外 JSON 错误。

identity 内部按真实职责分为 handler/validation、store 和 password,但没有 repository trait 或通用 service 抽象。只有出现第二个使用者时才提取接口。

身份请求路径

HTTP request
  -> Session middleware
  -> Axum identity handler / AuthenticatedUser extractor
  -> input validation
  -> password blocking task and/or SQLx user store
  -> PostgreSQL users + tower_sessions.session
  -> stable JSON response and optional Set-Cookie

注册在用户写入成功后建立 Session;登录在密码校验成功后轮换 Session ID; Profile 的认证提取器同时检查闲置有效性、7 天绝对期限和用户是否仍然存在。

数据库与进程边界

应用启动必须成功连接 PostgreSQL;连接失败时进程以非零状态退出。/ready 在运行时检查数据库查询能力。

Migration 是部署前显式步骤。应用启动不调用 sqlx migrate run,也不允许 Session Store 自行创建表。过期 Session 清理只删除数据,不改变 schema。