Building basic micro-service with GoRilla

aditya goel
9 min readMar 6, 2022

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.
  • Using Middleware to perform the object-validations.

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.
  • It can also handle Middleware, with the help of which we can do the stuff like Object-Validation & Authentication.
  • We would also not need the “ServeHTTP()” method with Gorilla framework.

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.
  • At line #22, we have created a NewRouter with the help of Mux Package.
  • At line #24, we have registered our GET method and created a Subrouter() with the help of our root router.
  • At line #27, we have registered our PUT method and created a Subrouter() with the help of our root router.
  • At line #28, we are reading the path-parameter being received in the request and the same shall be read as parameter “id”.
  • At line #30, we have registered our ADD/POST method and created a Subrouter() with the help of our root router.
  • At line #50, we are also listening to the Interrupt / Kill signals on the unbuffered blocking channel. We don’t have any other work to do in this particular main function and therefore we are happy to be blocked on this channel.

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.
  • At line #76, the returnType of the function “GetProducts()” is another struct: Products and we are simply returning the hard-coded list of products having 2 products in it. Simple & straightforward.
  • At line #28, note that the function “ToJson()”, we are basically making use of the NewEncoder() function and encoding the request thus received. In this case, response shall be written onto the ResponseWriter, post encoding.

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.
  • At line #45, we have defined a simple logic for getting the product with last identifier.
  • At line #42, we have appended (i.e. we have added new product to the data-store) to the current list of products.

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.
  • This is a public method and that’s why we were able to call this from outside this handler.
  • At line #24, we have also used an inbuilt function “Atoi()” in order to convert the string into a numeric integer.
  • Note that in below code, at line #35, 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 #7.

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.
  • Below method “findProduct()” returns three things : product itself, index of that product and Error, if any.
  • At line #63, we are now updating the product in our datastore finally.

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.
  • We also supplied the Http-Verb as PUT using “-X” option.
  • With above operation, we had updated the Name, Description, Price and Sku.

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.
  • At line #86, post unmarshalling, it sets that same request into the context.

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.
  • At line #34, we are taking the product from the request rather.

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.
  • At line #62, we are taking the product from the context stored within the request itself.

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 :-

--

--

aditya goel

Software Engineer for Big Data distributed systems