How nisibz/go-auth-tests works
Project Type and Purpose
• This is a Go-based RESTful API focused on user authentication and management. It demonstrates user registration, login (JWT-based), protected routes, profile CRUD, and secure password handling (bcrypt), all backed by MongoDB.
File Structure Analysis
• cmd/http/main.go: Entry point, bootstraps the HTTP server.
• internal/adapter: Adapter layer implementing configuration loading, HTTP handlers (Gin routes), logging (slog), and MongoDB data access (models + repositories).
• internal/core: Core business logic organized by Clean/Hexagonal Architecture:
• domain: Core entities (User) and domain-level errors.
• port: Interfaces (inbound and outbound ports for auth and user workflows).
• service: Implementations of business use-cases (registration, login, user management).
• util: JWT and bcrypt utilities.
• Infrastructure:
• Dockerfile, docker-compose.yml: Containerization & orchestration.
• .env.example: Environment variable template.
• go.mod, go.sum: Dependency management.
Key Components
a. Client: External caller (Postman, UI, other services) sending HTTP requests.
b. HTTP Server (Gin):
• Listens on configured port.
• Delegates to middleware (JWT validation) and request handlers.
c. Configuration Loader: Reads environment variables (.env) and provides typed config objects.
d. Router & Middleware:
• Defines /api/auth and /api/users endpoints.
• Applies logging and JWT-based auth middleware.
e. Handler Layer: Transforms HTTP requests into Core port calls (AuthPort, UserPort).
f. Service Layer (Core):
• Implements registration, login, and user CRUD.
• Uses JWT util to sign/verify tokens.
• Uses bcrypt util to hash/compare passwords.
g. Repository Layer:
• MongoDB connection manager.
• UserRepository implements persistence port.
• Converts between domain.User and MongoDB models.
h. MongoDB: External database for user data.
i. Logger (slog): Structured logging across all layers.
Relationships and Data Flow
1. Client → HTTP Server (request)
2. HTTP Server → Middleware (logging, JWT validation)
3. Middleware (JWT validated) → Handler
4. Handler → Core Service via defined port interface
5. Service → Util (bcrypt for passwords, JWT for tokens)
6. Service → Repository (CRUD operations)
7. Repository ↔ MongoDB
8. Response bubbles back up: DB → Repository → Service → Handler → HTTP Server → Client
Architectural Patterns and Principles
• Clean/Hexagonal Architecture: Core business logic is isolated behind port interfaces; adapters plug in for I/O.
• Single Responsibility: Config, logging, HTTP handling, business logic, and persistence each in separate packages.
• Dependency Inversion: Core defines interfaces; adapters implement them.
• Layered Security: Password hashing at service; JWT-based route protection via middleware.
Putting It All Together
• To its right, stack HTTP Server (Gin) and beneath it the Router & Middleware.