Building basic micro-service with GoRilla

  • Building a micro-service with help of GORILLA/MUX framework in GoLang.
  • Using Middleware to perform the object-validations.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Catching Up with the World — Go Modules in a Monorepo

Code Smell 65 — Variables Named After Types

10 Best Programming Languages to Learn in 2020 for Jobs and Future

A letter of transparency and my personal apologies to our 3Commas family; The road ahead is bright.

ServerLess and Kubernetes

Good news guys🎉

Do sweat the small stuff

The fast paced nature of internships and the takeaway lessons

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
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Reading Configuration Files in Go

Generics in Golang.

[Golang] Dealing with Errors in Golang

Golang: Start 7 web frameworks with same set of codes