Building basic micro-service with GoRilla

In case you are landing here directly, it’s strongly suggested that you go and read through this for fundamentals.

In this blog, we shall be looking majorly on following two concepts :-

  • Building a micro-service with help of GORILLA/MUX framework in GoLang.

Question:- What are the important features of GORILLA Framework ?

Answer:- Though “http.NewServeMux()” is quite good and powerful, but there are some bits & pieces missing in it. Gorilla framework have got much more refined MuxRouter.

  • It can fetch and extract path-parameters from the request-path. They can also parse Regular-Expressions for us.

Question :- Demonstrate the Restful Microservice with the help of Gorilla framework ?

Step #1.) Let’s start with our main program. Here are set of dependencies we have mentioned into our main program :-

Note that, we have used a public package “github.com/gorilla/mux” for the very purpose of using gorilla package.

Step #2.) Here is how the code of our main function looks like :-

  • At line #19, we have initialised a new handler, by passing our generic logger object to it. we are using the “NewProducts” method, in order to create an object of the Products handler.

Step #3.) Here is how our handler function looks like. Note that, in above step #2, we used the “NewProducts()” method. Here is how it looks like :-

Step #4.) Inside the Handler file, we now don’t need to write the “ServeHTTP()” method.We shall be defining the methods directly. Here is our GET method looks like. Note that, this method is a public method.

Note that in above code, at line #56, We are also making use of a utility method called “ToJson()”. The same has also been defined below in the subsequent steps.

Step #5.) Here is how the code of service-method “GetProducts()” from “data” package looks like :-

  • In below code, at line #17, it indicates that this field would not be returned in this JSON.

Note :- At line #10 above, we have defined a Struct, which have some identifiers with it. Below defined are the meanings to the various fields and tags defined in the STRUCT :-

Step #6.) Let’s now execute the code so far and test the output :-

Step #7.) Let’s now proceed further to POST :- Here is how, we have defined the method to ingest new products inside our productHandler :-

Note that in below code, at line #69, We are also making use of a utility method called “FromJson()”. With this method, we are reading a JSON and unmarshalling the same to an object. The same has already been defined above in step #6.

Step #8.) Note that in below code, at line #76, we are making use of service-method “AddSingleProduct()” from the data package. The same has been defined below :-

  • In below code, at line #40, we are fetching the next-identifier to be used with the record (product in this case)to be added to the local data-store.

Step #9.) Let’s now re-run the code and verify, whether POST is working :-

Meanwhile, let’s again fetch the products from our Micro-service. We should now have three products :-

Step #10.) Let’s now proceed further to PUT :- Here is how, we have defined the method to update an existing product inside our productHandler :-

  • Note that, this method doesn’t receive “id” in the request, rather it gets the value of the variable from the mux package.

Step #11.) As used in above code-piece, here is how the code of function “updateSingleProduct()” looks like, which comes from data package :-

  • In below code, at line #67, we are fetching the product (which has to be updated) from the underlying datastore. Recall that, we are also checking whether ID of the product received through the URL is same as that of record through which we are iterating.

Step #12.) Let’s now re-run the code and verify, whether PUT is working :-

  • Note that, we basically updated the product with id value as 1.

Let’s now again get the products from the micro-service using GET verb and here are results :-

Step #13.) In order to run the above code, we would require to import the gorilla/mux package :-

Post we run above command, here is how our “go.mod” file looks like :-

Post we run above command, here is how our “go.sum” file looks like :-

Question :- Demonstrate the practical use of Middleware with the help of Gorilla framework ?

Step #1.) Let’s start writing our middleware function.

  • At line #77, this function is very simple, as it just receives the request and unmarshall the request.

Step #2.) Now, we modify our method “UpdateSingleProduct()”.

  • We no more need to perform the unmarshalling in this method and we should limit ourselves only to have business logic inside our this method.

Step #3.) Now, we modify our method “AddProduct()”.

  • Similarly here as well, we no more need to perform the unmarshalling in this method and we should limit ourselves only to have business logic inside our this method.

Step #4.) Here is our main function looks like :- At line #29 and line #33, we have specified our intent to use our middelware method in order to perform the product validation.

Note that, middleware is the first method, even before the actual handler method is invoked. Here is deep dive into the “Use()” method at line #33 :-

Step #5.) Let’s now run our micro-service and perform the POST test. We can see that, JSON has been successfully un-marshalled and product has been formed from it.

Step #6.) Let’s now perform yet another PUT type of test, but this time with wrong/incorrect JSON. We can see that, JSON has NOT been un-marshalled :-

Step #7.) Let’s now perform yet another PUT type of test, but this time with correct JSON. We can see that, JSON has been successfully un-marshalled and received-record has been received from it :-

Here are our products now from GET call, which is working as usual :-

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store