How 880831ian/go-auth works
Project type and purpose
• This is a Go-based, single-service authentication API.
• It exposes HTTP endpoints (via Gin) for login, signup, token issuance, and token revocation.
• It persists user data in MySQL (GORM) and manages tokens/blacklists in Redis.
File structure
• cmd/main.go: application entry point and HTTP server bootstrap
• configs/: environment configuration loader (Viper)
• routes/router.go: Gin router and endpoint registration
• middlewares/: request-level filters (JWT auth, rate limiting, blacklist)
• controllers/auth_controller.go: HTTP handlers for auth flows
• services/auth_service.go: business logic (validate credentials, generate tokens)
• repositories/: data-access layer
• user_repository.go → MySQL via GORM
• token_repository.go → Redis for token storage/blacklist
• models/user.go: user schema
• utils/jwt.go: JWT creation and validation helpers
• docs/: Swagger spec for API documentation
a. Main components
• Clients (Mobile/Web)
• API Gateway / Load Balancer (optional)
• Auth Service (this application)
• HTTP Server (Gin)
• Router
• Middlewares
• Controllers
• Services
• Repositories
• Utils & Config
• MySQL Database
• Redis Cache
• Swagger UI (docs endpoint)
b. Component relationships & data flows
1. Client → HTTP Server (Gin)
2. Router dispatches to middlewares in order:
• RateLimit → Blacklist → JWT Auth
3. Controller → Service
4. Service → Repository
• For user records → MySQL
• For token read/write & blacklist → Redis
5. Service uses Utils (jwt.go) to sign/verify tokens
6. Config module injects DB/Redis connections at startup
7. /docs swagger.json → Swagger UI endpoint
c. Architectural patterns & principles
• Layered (HTTP → Controller → Service → Repository)
• Dependency injection of configs & DB/Cache clients
• Separation of concerns:
• Middlewares handle cross-cutting features (auth, rate limiting)
• Controllers only map HTTP ↔ service calls
• Services encapsulate business rules
• Repositories isolate persistence logic
d. Key technologies & libraries
• Gin (HTTP router)
• GORM + MySQL (ORM)
• Redis client
• golang-jwt/jwt for token handling
• Viper for config management
• swaggo for Swagger generation
• Rate limiter (golang.org/x/time/rate)
• Testify for unit tests
Detail is key
• Break the service into as many meaningful sub-components as shown in the file tree
• Boundary where a request or data passes from one layer to another
• Initialization flows (config → DB/Redis client → router setup) if helpful