Tutorial backend rest api work with frontend vue.js
Framework By Gin Web Framework
- 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
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
set module
go env -w GO111MODULE=on
go mod init
go mod tidy
go mod vendor
go mod verify
MONGODB_CONNECTION_STRING
- default : mongodb://127.0.0.1:27017
- example : mongodb://root:password@192.168.1.102:27017
PORT
- default : 8089
go run main.go
-
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"}
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