This repository provides a development environment that includes a Docker container defined in dockerfiles/Dockerfile.dev
.
You can choose one of the following methods to start the development environment:
-
Build the application:
make build
-
Run the application:
./todo_crud
-
Start the application using Docker Compose:
make docker-compose # or docker-compose up
This development environment uses github.com/cosmtrek/air
for live reloading, which automatically refreshes your application upon file save.
We enforce code standards using pre-commit
. Follow these steps to set up and use Git hooks:
-
Run the following command to set up Git hooks:
make git-hooks
-
To manually execute all Git hooks, use:
make lint
-
If you need to add or remove default hooks, update the
.pre-commit-config.yaml
file. -
To temporarily skip Git hooks, add the
--no-verify
flag to yourgit commit
orgit push
command. -
Customize the
.gitlint
file to define your team's Git message convention.
make tools
This will install the required tools for the application.
You can configure the microservice in two ways:
Create a todo_crud.yaml
file with your configuration values. For example, server.Config.Host: localhost
in the YAML file corresponds to server: host: localhost
.
By default, all environment variables are in UPPERCASE, and follow the same logic as the Method 1: Config File values.
The flow logic for handling requests in a Go service follows Clean Architecture principles and typically involves the following steps:
-
HTTP Request Handling (Handler Layer):
- Incoming HTTP requests are handled by functions located in the
internal/app/handler/
directory. Handlers
are responsible for handling incoming requests, parsing parameters, input validation/sanitization, and orchestrating the execution of use cases.
- Incoming HTTP requests are handled by functions located in the
-
Business Logic (Use Case Layer):
- Handlers delegate requests to use cases located in the
internal/app/usecase/
directory. Use cases
contain core business logic, such as orchestrating the execution for data retrieval, validation, and more.
- Handlers delegate requests to use cases located in the
-
Data Access (Repository Layer):
- Use cases interact with repositories located in the
internal/app/repository/
directory to fetch or update data from storage, often a database.
- Use cases interact with repositories located in the
-
Response Preparation (Use Case Layer):
- Use cases prepare response data structures or DTOs that match the API contract or HTTP response requirements.
-
HTTP Response Handling (Handler Layer):
- Handlers receive use case responses and format them into HTTP responses, including setting headers and encoding the response body.
-
Middleware (Middleware Layer):
- Middleware functions in the
internal/middleware/
directory can be applied at various points in the flow for tasks like authentication, authorization, logging, and error handling.
- Middleware functions in the
-
Error Handling:
- Error handling is crucial throughout the process. Errors can occur in handlers, use cases, or repositories. Proper error handling and error propagation to the appropriate layer are essential for robust applications.
- For a correct logging & error handling, the handlers should return a
&fiber.Error{}
This separation of concerns makes the codebase modular, easier to test, and maintainable. Additionally, it allows for easy component swapping (e.g., changing the database or switching to another protocol) since dependencies are abstracted through interfaces.