Overview
The SFLUV backend is a Go 1.24 HTTP API server using:- chi - Lightweight HTTP router
- pgx - PostgreSQL driver
- Privy JWT - Authentication via ES256 tokens
- Mailgun - Transactional email
- go-ethereum - Blockchain interaction
Project Structure
Startup Flow
main.go
backend/main.go:18-125
- Database connections initialized first
- Services created with database dependencies
- Router receives all services for handler registration
- Background services (affiliate scheduler) started as goroutines
Layered Architecture
Layer 1: Database (db/)
All SQL queries isolated in the db/ package. No handlers write SQL directly.
Example: backend/db/app_user.go
AppDB, BotDB, or PonderDB.
Layer 2: Handlers (handlers/)
HTTP handlers process requests, call database layer, return JSON.
Example: backend/handlers/app_user.go
AppService, BotService, PonderService).
Layer 3: Router (router/)
Routes map HTTP methods/paths to handlers with middleware guards.
Example: backend/router/router.go:56-65
Service Structure
AppService
Handles core application features (users, workflows, wallets, locations).backend/handlers/app.go:8-19
handlers/app_*.go files):
app_user.go- User CRUD, email verificationapp_workflow.go- Workflow creation, voting, step executionapp_wallet.go- Wallet managementapp_location.go- Merchant location CRUDapp_contact.go- Contact bookapp_affiliate.go- Affiliate role requestsapp_admin.go- Admin endpoints (user/role management)app_ponder.go- Ponder subscription management
BotService
Handles faucet operations and QR code redemptions.POST /events- Create faucet eventPOST /events/{id}/codes- Generate QR codesPOST /redeem- Redeem QR codeGET /balance- Faucet balance
W9Service
Manages W9 tax compliance (tracks earnings, submission, approval).POST /w9/submit- Submit W9 formPOST /w9/webhook- Receive W9 from WordpressPOST /w9/transaction- Record transaction (Ponder callback)GET /admin/w9/pending- Admin: list pending submissionsPUT /admin/w9/approve- Admin: approve submission
PonderService
Queries blockchain transaction history.GET /transactions- Get transfer history for addressGET /transactions/balance- Get balance at timestamp
Database Connections
Connection Pooling
backend/db/db.go
Background Services
AffiliateScheduler
Runs periodic payouts for affiliate events.BotService Background Jobs
Monitors faucet events and triggers QR code generation.Error Handling
HTTP Error Responses
Logging
backend/logs/prod/app.log with timestamps.
Dependencies
backend/go.mod:7-15
Testing
Run All Tests
Test Structure
Tests live alongside source files:backend/handlers/bot_redeem_code_test.go
Next Steps
Handlers
Deep dive into handler structure and patterns
Middleware
Authentication and role-based guards
Router
Route definitions and organization