Create a basic distributed system in Go lang – Part 2- http server & JSON request/response

json-rest3

Introduction

So this is a part 2 of a series of posts I am planning to write to create a very basic distributed system in Go. I am adding new features as I keep exploring the topic and as I read new papers on the subject. The posts are sometimes slow just because I am not putting a lot of dedicated time on it yet but this may improve over time πŸ™‚

What are we building

In the part 1 of the series we built a simple server that can communicate with other nodes in the cluster over TCP. The first node starts as a master and other nodes connect to the existing node and communicate with each other. This part we will expose

  • a minimal HTTP endpoint that can be used to query the cluster and serve json responses
  • a minimal client that can send/receive json messages to the http endpoint

How are we planning to use this in future

Well the simple plan I have in mind now is that all the server nodes communicate to other nodes over the TCP and any client/web-interface communication would happen over the HTTP. Note: we could had used either of them to handle all the communications as well, I am just breaking this down to explore both the communication mechanisms.

Getting to the code

Messages

Messages are the useful structs we will be using to exchange all out future json messages. We will keep on improvising these as we keep using them.

Http Server

This is the main server code that exposes multiple endpoints and receives requests and serves appropriate response back to the client. We currently create two endpoints here.

Client

This is a minimal client that listens continously for messages on the console and sends the messages to the server every time we hit a newline. The messages are send as Json and prints the received message back on console.

Running the code

The code can be tested in two tabs of your console by these commands

Start Http server :
go run src/go-going/gone/http/main.go --clusterip 127.0.0.1:8001

Start Client & type input:
go run src/go-going/gone/client/main.go --clusterip 127.0.0.1:9001

Testing the Http server

We can use chrome extension Postman which is really popular for testing web-api’s. We will query our 2 http endpoints and check the response back.

The top part of the screenshots show the request made and the bottom part shows the response received back-

1. localhost:9001 : This is a hello world endpoint that is supposed to get the details of the node connected or, can be used to fetch the health status of our cluster. For now it just displays nothing in the node info.

screen-shot-2016-10-15-at-1-26-18-pm

2. localhost:9001/query : This endpoint expects a json message with a user query and sends back a message back to the client.

screen-shot-2016-10-15-at-1-26-04-pm

Note:Β in the above is the body of the request where we changed type to JSON and added the message in body.

Hope this really small post was helpful. Will keep on adding more posts as the client matures. You can find all the code I have come up with here.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *