Go + Gin + MongoDB = gogimon
This is a simple and minimal template for building RESTful APIs using Gin as HTTP web framework and MongoDB as database in Go.
This template follows a dependency injection pattern to make the code modular and testable. This template will help you to spin up a new project quickly and focus on building your application logic.
It contains a sample CRUD structure with a User
entity, including routes, handlers, repositories and data transfer objects (DTOs). Easily dettachable code for developing business logic and automated testing.
- gin-gonic/gin: Uses Gin, a HTTP web framework for Go, useful for fast building APIs.
- mongodb/mongo-go-driver: Integrates MongoDB for data storage.
- testcontainers/testcontainers-go: Includes Testcontainers to simplify integration testing and enable testing with external services like databases, Kafka, or Redis. In this template, it is used to run a MongoDB container for testing.
- go-playground/validator: Uses validator to validate DTOs.
- actions/setup-go: Uses GitHub Actions for build and testing application.
- Go: Make sure you have Go installed on your machine.
- Docker: Required to run MongoDB container for both development and testing.
- Air: Recommended for live reloading during development.
You may want to run the application in a container for a fast setup:
git clone https://github.com/ericbsantana/gogimon.git
cd gogimon
docker-compose up -d
The application will be available at http://localhost:8080
. The routes are defined in main.go
:
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "OK")
})
r.GET("/users", userHandler.Find)
r.GET("/users/:id", userHandler.FindByID)
r.POST("/users", userHandler.Create)
r.PATCH("/users/:id", userHandler.Update)
r.DELETE("/users/:id", userHandler.Delete)
If you prefer to run the application locally, you need to have MongoDB running in a container or nativelly on your machine.
You have to set the environment variables MONGODB_URI
to connect to your MongoDB instance. You can set them in a .env
file in the root of the project:
# in project root
cp .env.example .env
Now you can run the application:
air
...
[GIN-debug] Listening and serving HTTP on :8080
...
Open http://localhost:8080
and you should see a 200 OK
response.
.
βββ Dockerfile # for building the application and run with compose
βββ docker-compose.yml # to run the application with a MongoDB container
βββ main.go # entry point of the application
βββ internal
βββ databases # contains mongo database connection
βββ dtos # data transfer objects, createUserDto, for example
βββ handlers # request handlers or controllers, call it what you want
βββ models # data models, like User
βββ repositories # data access layer to interact with the database
βββ tests # integration tests
βΒ Β βββ config # configuration of a testcontainer for MongoDB
βΒ Β βββ handlers # handlers tests
βββ validator # custom validators for DTOs
This template includes integration tests using Testcontainers. To run the tests, use the following command:
go test ./...
Contributions are welcome! Feel free to open issues or pull requests to suggest improvements, report bugs, or add new features.
Here's what's planned for future development of this template:
-
Authentication with JWT: Implement JWT-based authentication to secure endpoints and manage user sessions.
-
Logging Mechanism: Introduce a logging mechanism to provide insight into the application's behavior.
This project is licensed under the MIT License.