Skip to content
/ gogimon Public template

πŸ“¦ A simple and minimal Go boilerplate for building RESTful APIs using Gin as HTTP web framework and MongoDB as database. Equipped with Testcontainers to make integration tests and expand to test other modules like Kafka or Redis.

License

Notifications You must be signed in to change notification settings

ericbsantana/gogimon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GitHub go.mod Go version Go Report Card

Boilerplate: Go + Gin + MongoDB

Go + Gin + MongoDB = gogimon

Overview

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.

Features

Pre-requisites

  • 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.

Quick Start

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)

Manual Setup with Air

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.

Project folder structure

.
β”œβ”€β”€ 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

Testing

This template includes integration tests using Testcontainers. To run the tests, use the following command:

go test ./...

Contributing

Contributions are welcome! Feel free to open issues or pull requests to suggest improvements, report bugs, or add new features.

Roadmap

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.

License

This project is licensed under the MIT License.

About

πŸ“¦ A simple and minimal Go boilerplate for building RESTful APIs using Gin as HTTP web framework and MongoDB as database. Equipped with Testcontainers to make integration tests and expand to test other modules like Kafka or Redis.

Topics

Resources

License

Stars

Watchers

Forks