Skip to content

tarathep/tutorial-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tutorial-backend

Tutorial backend rest api work with frontend vue.js

Framework By Gin Web Framework

prerequisites

  • Go 10.x have been Installed
  • MongoDB Available
    docker run -it --rm --name mongodb -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=password mongo:latest

go dependencies

set go module

go env -w GO111MODULE=auto

gin gonic

go get -u github.com/gin-gonic/gin

mongodb

go get -u go.mongodb.org/mongo-driver/mongo

testify

go get -u github.com/stretchr/testify

Go Module

set module

go env -w GO111MODULE=on

go mod init

go mod tidy

go mod vendor

go mod verify

Environment Variables

  • MONGODB_CONNECTION_STRING
    • default : mongodb://127.0.0.1:27017
    • example : mongodb://root:password@192.168.1.102:27017
  • PORT
    • default : 8089

Run

go run main.go

APIs

  • GET : /api/tutorials

    response body

    [{"id":"602aa1e04f3b51804eca6917","title":"yy","description":"xx Description","published":false,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"},{"id":"602aa1e04f3b51804eca6917","title":"yy","description":"xx Description","published":false,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}]
  • GET : /api/tutorials/602aa1e04f3b51804eca6917

    response body

    {"id":"602aa1e04f3b51804eca6917","title":"yy","description":"xx Description","published":false,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}
  • POST : /api/tutorials

    request body

    {"title":"xx","description":"xx Description"}

    response body

    {"code":"200","message":"Inserted a single document Success"}
  • PUT : /api/tutorials

    request body

    {"id":"602aa1e04f3b51804eca6917","title":"yy","description":"xx Description","published":false,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}

    response body

    {"code":"200","message":"Updated  a single document Success"}
  • DELETE : /api/tutorials

    response body

    {"code":"200","message":"All deleted"}
  • DELETE : /api/tutorials/602aa1e04f3b51804eca6917

    response body

    {"code":"200","message":"Deleted id 602aa1e04f3b51804eca6917"}

Unit Test

test

go test

test with create cover profile

go test -coverprofile coverage.out ./...

export report html

go tool cover -html coverage.out -o report.html

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages