Skip to content

powerOFMAX/golang-crud

Repository files navigation

[Go/Docker] Gin-gonic & MongoDB Messages Backend

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

Tree

├── 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

Available Endpoints

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

Setup the project

First you must setup your database credentials. You can use .env-example as reference.

$ cp .env-example .env

Using with Docker

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

Example

After running docker-compose open:

Without docker

$ make serve

You may need to execute go mod download in src folder first

Configure scripts

./scripts/production.sh

cd src 
go mod download
go build -o /bin/app && /bin/app

./scripts/development.sh`

cd src
go run main.go

About

Golang Crud using Gin-gonic / Docker / MongoDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published