1---2name: backend-rust3description: Rust gRPC backend patterns, folder structure, module anatomy, and infrastructure wiring for the Anachak multi-tenant SaaS platform.4---56# Backend Rust Skill78Comprehensive guide for working with the **Anachak Rust** gRPC backend — a multi-tenant SaaS platform with 11 modules, 32 utility packages, and 90+ RPCs.910## Tech Stack1112| Layer | Technology | Version |13|---|---|---|14| Language | Rust | 2021 edition |15| Transport | gRPC (Tonic) + gRPC-Web | tonic 0.12 |16| Database | PostgreSQL (SQLx) | sqlx 0.8 |17| Proto | Protocol Buffers v3 | Buf CLI v2 |18| Auth | JWT (jsonwebtoken 9) | Access + Refresh |19| Password | Argon2 (argon2 0.5) | PHC-format |20| Payments | Bakong/KHQR (EMV-compliant) | reqwest 0.12 |21| Metrics | Prometheus | prometheus 0.13 |22| WebSocket | tokio-tungstenite 0.24 | Notification broadcast |23| OTP | TOTP (totp-rs 5) | Google Authenticator |24| SMS | Plasgate | Cambodia gateway |25| Email | Resend | HTTP API |26| IDs | ULID (ulid 1) | Sortable, unique |27| Cache | In-memory / Redis (optional) | Feature-gated |28| Observability | tracing + optional OpenTelemetry | Feature-gated `otel` |29| Hot Reload | cargo watch | `cw` alias |3031## Quick Reference3233| Topic | File |34|---|---|35| Folder structure | [folder-structure.md](./folder-structure.md) |36| Module anatomy | [module-anatomy.md](./module-anatomy.md) |37| Infrastructure utils | [infrastructure.md](./infrastructure.md) |38| Interceptor/layer chain | [interceptors.md](./interceptors.md) |39| Database patterns | [database-patterns.md](./database-patterns.md) |40| Error handling | [error-handling.md](./error-handling.md) |41| Adding a new module | [new-module-checklist.md](./new-module-checklist.md) |42| Proto & Buf | [proto-and-buf.md](./proto-and-buf.md) |4344## Module Summary4546| Module | Files | Key Responsibility |47|---|---|---|48| **auth** | 4 (`auth.rs`, `auth_repo.rs`, `auth_service.rs`, `mod.rs`) | User accounts, JWT, 2FA, OAuth, sessions, audit |49| **company** | 4 | Multi-tenant company/branch/member management |50| **businesstype** | 4 | Industry presets (28 types), pricing tiers |51| **billing** | 4 | KHQR payments, Bakong polling, streaming |52| **coupon** | 4 | Coupon creation, validation, redemption tracking |53| **role** | 4 | RBAC role/permission management |54| **subscription** | 4 | Plan lifecycle, branch-scoped subscriptions |55| **notification** | 4 | In-app notification delivery |56| **currency** | 4 | Multi-currency exchange rates |57| **activity** | 4 | Audit activity logging |58| **admin** | 4 | Cross-cutting admin operations |5960## Entrypoints6162| File | Purpose |63|---|---|64| `main.rs` | Calls `internal::startup::run()` |65| `internal/startup/mod.rs` | Server bootstrap — env, DB, utils, gRPC server, HTTP, cron, shutdown |66| `internal/startup/proto.rs` | Proto module re-exports & file descriptor set |67| `internal/startup/events.rs` | Event handler registration |68| `internal/startup/cron_jobs.rs` | Cron job scheduling |69| `internal/startup/http_server.rs` | Side-channel HTTP (metrics + WebSocket) |70| `build.rs` | tonic-build proto compilation |7172## Ports7374| Port | Protocol | Purpose |75|---|---|---|76| `50050` | gRPC | Native gRPC + gRPC-Web |77| `9090` | HTTP | Prometheus metrics |78| `9091` | WebSocket | Real-time notification broadcast |7980## Key Patterns81821. **Repository-Service** — Every module: `<module>.rs` (types) → `<module>_repo.rs` (data) → `<module>_service.rs` (business logic + gRPC)832. **Arc-based dependency injection** — All shared services wrapped in `Arc<T>` and passed explicitly843. **gRPC-first** — Proto is source of truth; gRPC-Web enabled for browser clients854. **Tower layer chain** — 7 layers for cross-cutting concerns (recovery, timeout, logging, etc.)865. **Tonic interceptor** — `AuthInterceptor` validates JWT + blacklist, injects user context into metadata876. **Graceful shutdown** — `ShutdownCoordinator` manages ordered teardown of 5 components887. **Forward-only SQL migrations** — Sequential `.sql` files in `migrations/`898. **Event-driven side effects** — `EventBus` for decoupled reactions to domain events909. **Feature flags** — PostgreSQL-backed with 60s in-memory refresh9110. **Unified error handling** — `AppError` enum → auto-converts to gRPC `Status` with `x-error-code` metadata