This is a basic CRUD sample using Gin-gonic and MongoDB. The project has a docker configuration if you don't want to install go globally.
For this project I used MongoDB Atlas But you can setup a local Mongo Database if you want to.
# messages collection
_id: ObjectId
title: String
content: String
author: String
├── Dockerfile
├── Makefile
├── README.md
├── cmd
│ └── app
│ └── server.go
├── docker-compose.yml
├── go.mod
├── go.sum
├── pkg
│ ├── config
│ │ └── db.go
│ ├── controllers
│ │ └── messages.go
│ └── routes
│ └── routes.go
└── scripts
├── development.sh
└── production.sh
[GET] /api/v1/ - Welcome (To test the Endpoint)
[GET] /api/v1/messages - Get All Messages)
[POST] /api/v1/message - (Create Message)
[GET] /api/v1/message/:messageId - (Get Single Message)
[PUT] /api/v1message/:messageId - (Edit Message)
[DELETE] /api/v1/message/:messageId - (Delete Message)
You can test the endpoints using Postman. I left a collection in the project.
First you must setup your database credentials. You can use .env-example
as reference.
$ cp .env-example .env
Start production enviroment
$ docker-compose up -d production
// View logs
$ docker-compose logs --tail 100 -f production
Start development enviroment
$ docker-compose up development
Re-building docker
$ docker-compose build --no-cache
Attach to bash
$ docker-compose exec <production|development> sh
After running docker-compose open:
-
development: http://localhost:8080/
-
production: http://localhost:8081/
$ make serve
You may need to execute go mod download
in src
folder first
cd src
go mod download
go build -o /bin/app && /bin/app
cd src
go run main.go