This project is a 'Boilerplate' or 'Starter' to build RESTful Applications and microservices using GoLang (Gin HTTP web framework), MongoDB and Redis.
- Gin
- JWT Authentication
- DotEnv with Viper
- Request Validation with Ozzo Validation
- MongoDB ODM (mgm)
- Redis Cache
- Docker support
- Swagger with gin-swagger
- Git
- Docker
- Go 1.20 and above
If you are not using Docker, you should also have:
- MongoDB
- Redis 6.2.X
Clone the repository and enjoy, I strongly recommend Jetbrains GoLand :)
# Clone Project
git clone https://github.com/ebubekiryigit/golang-mongodb-rest-api-starter.git go-rest-api-starter
# Change Directory
cd go-rest-api-starter
# Build & Create Docker Containers
docker-compose up -d
# Copy Example Env file
cp ./env.example .env
# Change MongoDB/Redis URI and Database Name
# MONGO_URI=<mongo_uri>
# MONGO_DATABASE=<db_name>
# USE_REDIS=<true or false>
# REDIS_DEFAULT_ADDR=<redis_uri>
# Download Modules
go mod download
# Build Project
go build -o go-starter .
# Run the Project
./go-starter
The application starts at port 8080:
GET /v1/ping
Health check endpoint, returns 'pong' message
POST /v1/auth/register
Creates a user and tokensPOST /v1/auth/refresh
Refresh expired tokensPOST /v1/auth/login
Login a user
POST /v1/notes
Create a new noteGET /v1/notes
Get paginated list of notesGET /v1/notes/:id
Get a one note detailsPUT /v1/notes/:id
Update a noteDELETE /v1/notes/:id
Delete a note
GET /swagger/*
Auto created swagger endpoint
You can also see: http://localhost:8080/swagger/index.html
If you want to add new routes and swagger docs, you should run
swag init
See: Gin Swagger
├── controllers # contains api functions and main business logic
├── docs # swagger files
├── logs
├── middlewares # request/response middlewares
│ └── validators # data/request validators
├── models
│ └── db # collection models
├── routes # router initialization
└── services # general service & database actions
- Rate Limiting
- i18n Localization
- Testing