How amitshekhariitbhu/go-backend-clean-architecture works
Project Type and Purpose
• This is a backend web service application following Clean Architecture. It clearly separates concerns, ensuring that routing, controllers, business logic (use cases), and data access (repositories) are decoupled, promoting testability and maintainability.
• Key features include JWT-based authentication, RESTful API endpoints (e.g., signup, login, profile, task management), and an external MongoDB database for persistence. Docker is also used for containerized deployments.
File Structure Insights
• Top-level directories such as "api", "bootstrap", "cmd", "domain", "repository", and "usecase" indicate a layered or hexagonal architecture.
• The "api" directory contains subdirectories for controllers (handling HTTP requests), routes (defining the API endpoints), and middleware (like the JWT authentication middleware).
• The "domain" folder contains core domain models and error handling, which represent the business entities.
• The "usecase" directory encapsulates the business logic whereas the "repository" handles data interactions with MongoDB.
• The "bootstrap" and "cmd" directories include the initialization code (setting up the application, environment, and DB connections) and the main entry point respectively.
• Testing is supported through mocks found in the "domain/mocks", "mongo/mocks", and the test files scattered in the controller and usecase directories.
a. Core Layers:
• Front Controller/Router Layer: Represent this with the "api/route" component. It receives HTTP requests and directs them to the appropriate controllers.
• Controller Layer: In "api/controller", each controller (login, profile, refresh token, signup, task) receives requests routed in and handles the immediate translation between the HTTP layer and the business logic.
• Middleware (JWT): Show a specific middleware block (from "api/middleware") for ensuring secure access.
• Usecase Layer: Capture the business logic contained in "usecase" – these components process business rules and orchestrate domain operations.
• Repository/Data Access Layer: Highlight the "repository" components that interface with MongoDB. Also include the "mongo" package which defines the connection and mocks.
• Domain Layer: Represent core business entities and data structures in the "domain" folder.
• Infrastructure/Bootstrap: Include the "bootstrap" section (app.go, database.go, env.go) that initializes the application.
• Entry Point: Mark the "cmd/main.go" as the starting point for the application.
Relationships and Data Flow
• HTTP Client → Router (gin framework through the "api/route") → Middleware (JWT authentication).
• Middleware → Controller – Controllers (in "api/controller") then invoke specific use case functions.
• Controller → Usecase – Use cases handle business rules and interact with repositories.
• Usecase → Repository – The repository layer interacts with the database (defined in "mongo" and "repository").
• Repositories return data/confirm transactions → Usecase sends response back to Controller → Final response returned to the Client.
• Error flow pathways (e.g., error responses from the domain layer).
Architectural Patterns and Key Technologies
• This design leverages Clean Architecture principles: decoupling dependencies, clear layer separation, and ensuring that business logic is independent of frameworks and delivery mechanisms.
• Gin as the HTTP web framework.
• MongoDB as the primary database (with the mongo go driver).
• JWT libraries for authentication.
• Viper for configuration management.
• Docker for containerization.
• Testing tools like Testify and Mockery that are used within the mocks directories, indicating an emphasis on test-driven development.