In this article, I will show you how to create a RESTful CRUD(Create, Read, Update, Delete) application using Go language and MongoDB as database.
Pr-Requisites:
-------------------
a) Install go in your system(mac).
brew install go
brew upgrade go
b) Edit ~/.bash_profile and set the below:
export GOPATH=/Users/malark/work
export GOBIN=$GOPATH/binå
export GOROOT=/usr/local/Cellar/go/1.13.1/libexec
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
c) Install mongodb as a docker container:
Please see the article, "Getting Started With MongoDB As A Docker Container Deployment"
https://www.thepolyglotdeveloper.com/2019/01/getting-started-mongodb-docker-container-deployment/
Run the command "docker ps", you should see the output as below :
Malars-MacBook-Pro:~ malark$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6173fcf6fcd mongo:latest "docker-entrypoint.s…" 11 hours ago Up 11 hours 0.0.0.0:27017-27019->27017-27019/tcp mongodb
Now, lets create a RESTful application that performs CRUD operation using mongodb as database.
d) Create directory structure:
/Users/malark/work/src/
Install Mongodb driver for go:
go get github.com/gorilla/mux
go get go.mongodb.org/mongo-driver/mongo
e) Create a source code directory for your program:
github.com/makandas/restapi
f) List all directories under /Users/malark/work/src/ directory:
malars-mbp:src malark$ ls
github.com go.mongodb.org google.golang.org k8s.io sigs.k8s.io
github.ibm.com golang.org gopkg.in
Now lets, see the code:
Here is the code that connects to Mongodb:
-----------------------------------------------------------------------------
func main() {
fmt.Println("Starting the application...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(ctx, clientOptions)
router := mux.NewRouter()
router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET")
router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET")
router.HandleFunc("/person", CreatePersonEndpoint).Methods("POST")
router.HandleFunc("/people/{firstname}", UpdatePersonEndpoint).Methods("PUT")
router.HandleFunc("/people/{firstname}", DeletePersonEndpoint).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
}
----------------------------------------------------------------------------
You can find the complete code here: https://github.com/MalarvizhiK/restapi/mongo_restproject.go
Run the project:
--------------------------------------------------------------------------------
malars-mbp:restapi malark$ go run mongo_restproject.go
Starting the application...
.........
--------------------------------------------------------------------------------
a) Create a Person:
Use Postman to create a Person:
URL : http://localhost:8080/person
Method: POST
Body:
{
"firstname": "Vasant",
"lastname": "Sivashanmugam"
}
Status:200, Output:
b) Update a Person:
URL : http://localhost:8080/person/Vasant
Method: PUT
Body:
{
"lastname": "Siva"
}
Status: 200, Output:
c) Get a specific Person using id:
URL : http://localhost:8080/people/5db6fb9e561a9b24a04e5dd3
Method: GET
Status: 200, Output:
d) Delete a Person:
e) Get all Person:
URL : http://localhost:8080/people
Method: GET
Status: 200, Output:
References:
-----------------
https://www.thepolyglotdeveloper.com/2019/01/getting-started-mongodb-docker-container-deployment/
https://www.thepolyglotdeveloper.com/2016/07/create-a-simple-restful-api-with-golang/
https://www.thepolyglotdeveloper.com/2019/02/developing-restful-api-golang-mongodb-nosql-database/
https://github.com/tfogo/mongodb-go-tutorial
https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial
https://kb.objectrocket.com/mongo-db/how-to-update-a-mongodb-document-using-the-golang-driver-458