simple-webserver-with-REST example
Adding REST (GET, POST, PUT, DELETE) JSON API to my
simple-webserver.
Table of Contents,
GitHub Webpage
WHAT IS REST
REST (REpresentational State Transfer) is just a standard way for
computers to communicate over the web.
It is stateless, meaning who cares what the computer is doing at the time.
There are 4 basic HTTP verbs
we use in requests to
interact with resources in a REST system,
- GET - Retrieve a specific resource by id or a collection of resources
- POST - Create a new resource
- PUT - Update a specific resource by id
- DELETE - Remove a specific resource by id
REST vs TCP/IP
REST does not have state, whereas tcp has an open connection and you
can assume a lot about the server.
HIGH-LEVEL VIEW OF CODE
For simplicity, the code is broken into,
- simple-webserver-with-REST.go - Kicks off webserver
- router.go - The gorilla router
- routers.go - The list of the routes (e.g. /postdata)
- handlers - The functions to handle the routes
- mockdatabase.go - A slice of structs
- logger.go - A log wrapper for better output
USING A ROUTER - GORILLA/MUX
You will need to get github.com/gorilla/mux
which is
a popular package for writing web handlers.
go get -u -v github.com/gorilla/mux
MOCKDATABASE
A simple mock database has been set up,
TodoStruct{ID: "10", Name: "Write presentation", Completed: false},
TodoStruct{ID: "20", Name: "Eat Lunch", Completed: false},
TodoStruct{ID: "30", Name: "Pick up Milk", Completed: true},
RUN
go run simple-webserver-with-REST.go \
router.go routes.go handlers.go mockdatabase.go logger.go
NORMAL WEBPAGE (GUI)
In a browser,
127.0.0.1:1234
Will also show entire database.
GET
127.0.0.1:1234/getdata/20
{"id":"20","name":"Eat Lunch","completed":false}
POST (add)
This will add data,
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Feed Cat", "Completed": false}' \
http://127.0.0.1:1234/postdata/40
Check it was added,
127.0.0.1:1234/getdata/40
PUT (replace/update)
This will replace/update data,
curl -X PUT \
-H "Content-Type: application/json" \
-d '{"name":"Feed Cat", "Completed": true}' \
http://127.0.0.1:1234/putdata/40
DELETE
curl -X DELETE \
-H "Content-Type: application/json" \
http://127.0.0.1:1234/deletedata/20