How leoliscanoa/go-rest-api works
Main Components
• HTTP Server & Routes: Represented by the “routes” directory and main entry point (main.go), which initializes the application and binds URL patterns.
• Controllers: The “controller” directory (e.g., user.controller.go) handles HTTP requests and responses.
• Services: Contained in the “service” directory, these encapsulate the business logic. Note that there are both service implementations and test mocks.
• Repositories: Found under “repository” that abstracts data access. This includes both implementations (using Gorm for Postgres) and mocks.
• Models & DTOs: The “model” directory holds domain models (e.g., user.model.go), while “dto” provides data transfer objects.
• Middleware: Located in the “middleware” directory, these add cross-cutting concerns (e.g., basic authentication or logging).
• Configuration: The “config” directory deals with database and application configuration (including separate mock configuration).
• Documentation: The “docs” folder contains Swagger-related files (swagger.json/yaml) along with application documentation.
• Helper: Provides utility functions (like those in helper/response.helper.go) for constructing responses.
Relationships and Interactions
• A client request flows from the HTTP Server (as initiated in main.go, leveraging routes) to a specific controller.
• The controller validates input and then delegates to a service.
• The service processes business logic and, if needed, interacts with a repository to either query or update the database.
• Repositories use Gorm to communicate with Postgres (managed via Docker Compose) for persistent storage.
• Middleware intercepts requests for tasks like authentication or logging before they reach the controller.
• The configuration layer provides necessary runtime settings (such as database credentials) to the rest of the system.
• Swagger documentation is generated/inputted for API endpoint discovery and testing, visible at the provided Swagger UI link.
Architectural Patterns & Design Principles
• Layered (or N-tier) Architecture: Clearly divided into controllers, services, and repositories.
• Repository Pattern: Separating data access logic from business logic.
• Use of Mocks: Testing structure is evident from the presence of mock implementations in both repository and service layers.
• Containerization: With Docker (and docker-compose), the deployment is designed to abstract infrastructure dependencies (including Postgres).
• API Documentation Integration: Use of Swagger for API documentation.
Relevant Technologies & Frameworks
• Go (version 1.21.7)
• Mux for HTTP routing
• Docker & Docker Compose for container orchestration
• Postgres (accessed via Gorm ORM, version 1.25.7)
• Sqlmock and Testify for testing
• Swaggo for generating Swagger documentation