Go Backend Clean Architecture
Introduction
The "Go Backend Clean Architecture" project is a well-structured template designed for building backend applications in Go (Golang). It leverages the Gin web framework, integrates with MongoDB, and implements JWT authentication for secure API access. This project is designed to serve as a baseline for developers looking to create scalable and maintainable backend services.
Key Features
- Based on Clean Architecture: The project adopts the clean architecture principles to ensure separation of concerns, making the codebase easier to understand and modify.
- Modular Design: It separates the core logic into components like Router, Controller, Usecase, Repository, and Domain.
- JWT Authentication: Secure your APIs using middleware for JWT access token validation.
- Dockerized Environment: Easily run the entire application in Docker containers, ensuring consistent environments across different machines.
Project Architecture
The architecture is divided into several layers:
- Router: Handles HTTP requests, routes them to necessary controllers.
- Controller: Contains logic to handle requests, responds to routes by calling use cases.
- Usecase: Contains business logic and interacts with repositories.
- Repository: Data access layer that interacts with the database.
- Domain: Represents the core application logic and business rules.
Running the Project
The project can be executed either with or without Docker:
- Without Docker: Proper installation of Go and MongoDB on the local machine is required. Update the
.env
file with your configurations and rungo run cmd/main.go
. - With Docker: Ensure Docker is installed, configure the
.env
file, and executedocker-compose up -d
to launch the application.
Testing and Mocking
Testing is made straightforward with packages like testify
for common assertions and mockery
for generating mock interfaces. Run tests using go test ./...
and generate mock code with command-line utilities.
Major Packages
- Gin: Fast and lightweight web framework for handling HTTP requests.
- Mongo Go Driver: Official MongoDB driver for interfacing with the database.
- JWT: Enables secure and verifiable claims between parties using JSON web tokens.
- Viper: Handles configuration management, enabling the application to read settings from various sources.
- Bcrypt: Implements password hashing for enhanced security.
Example API Requests
- Signup: Create a new user account.
- Login: Authenticate a user and receive JWT tokens.
- Profile: Retrieve user profile information using the access token.
- Task Management: Supports task creation and retrieval.
Conclusion
"Go Backend Clean Architecture" serves as a robust starting point for developers wishing to create efficient and maintainable backend services using modern practices. With its focus on clean separation and modular design, it promotes best practices in API development and lifecycle management.
For more detailed documentation and to explore more about this architecture, visit the API documentation.
Feel free to contribute to the project, and for any improvements or suggestions, feedback is always welcomed to enhance the overall experience.