| title | Clean Architecture | |||||
|---|---|---|---|---|---|---|
| keywords |
|
|||||
| description | Implementing clean architecture in Go. |
This example demonstrates a Go Fiber application following the principles of Clean Architecture.
This project provides a starting point for building a web application with a clean architecture. It leverages Fiber for the web framework, PostgreSQL for the database, and follows the Clean Architecture principles to separate concerns and improve maintainability.
- Go 1.18 or higher
- PostgreSQL
- Git
api/: Contains the HTTP handlers, routes, and presenters.build/: Contains the built application.cmd/: Contains the main application entry point.db/: Contains the database migration.internal/: Contains internal application package.pkg/: Contains the core business logic and entities.
-
Clone the repository:
git clone https://github.com/ABA-Developer/BE-dashboard-nba.git cd BE-dashboard-nba -
Set the environment variables in a
.envfile:DB_MIGRATOR_DRIVER="postgres" DB_USERNAME="auth_user" DB_PASSWORD="auth_user" DB_NAME="auth_user" DB_HOST="localhost" DB_PORT="5432" DB_SSLMODE="disable" DB_MAX_OPEN_CONNS=10 DB_MAX_IDLE_CONNS=10 DB_MAX_IDLE_TIME=5m DB_MAX_LIFETIME=5m DB_MAX_CONN_WAIT_TIME=5m DB_MAX_CONN_LIFETIME=5m DB_MAX_CONN_IDLE_TIME=5m DB_ADDR="postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE}"
-
Install the dependencies:
go mod download
-
Run the application:
make run
The API should now be running on http://localhost:8080.
The following endpoints are available in the API:
- GET /user: List all users.
- POST /user: Add a new user.
- GET /user/:id: Get a user by id.
- PUT /user/:id: Update an existing user by id.
- DELETE /user/:id: Remove a book by id.
Clean Architecture is a software design philosophy that emphasizes the separation of concerns, making the codebase more maintainable, testable, and scalable. In this example, the Go Fiber application follows Clean Architecture principles by organizing the code into distinct layers, each with its own responsibility.
- Entities (Core Business Logic)
- Located in the
pkg/entitiesdirectory. - Contains the core business logic and domain models, which are independent of any external frameworks or technologies.
- Use Cases (Application Logic)
- Located in the
pkg/userdirectory. - Contains the application-specific business rules and use cases. This layer orchestrates the flow of data to and from the entities.
- Interface Adapters (Adapters and Presenters)
- Located in the
apidirectory. - Contains the HTTP handlers, routes, and presenters. This layer is responsible for converting data from the use cases into a format suitable for the web framework (Fiber in this case).
- Frameworks and Drivers (External Interfaces)
- Located in the
cmddirectory. - Contains the main application entry point and any external dependencies like the web server setup.
- Entities: The
entities.Userstruct represents the core business model for a user. - Use Cases: The
user.Serviceinterface defines the methods for interacting with user, such asCreateUser,UpdateUser,DeleteUser, andGetUserById. - Interface Adapters: The
handlerspackage contains the HTTP handlers that interact with theuser.Serviceto process HTTP requests and responses. - Frameworks and Drivers: The
cmd/main.gofile initializes the Fiber application and sets up the routes using theroutes.UserRouterfunction.
By following Clean Architecture principles, this example ensures that each layer is independent and can be modified or replaced without affecting the other layers, leading to a more maintainable and scalable application.
This example provides a basic setup for a Go Fiber application following Clean Architecture principles. It can be extended and customized further to fit the needs of more complex applications.