Simplify Your REST API Responses with Milogo for Gin-Gonic

manuelarte

Manuel Doncel Martos

Posted on November 18, 2024

Simplify Your REST API Responses with Milogo for Gin-Gonic

Gin-Gonic Middleware that implements fields selection pattern

At my company we use Go to build internal tools. Recently I worked in a REST API using gin-gonic, that required displaying a lot of data across many endpoints.

One must have feature in this type of scenario, is pagination, but and often overlooked pattern is partial response (a.k.a field selection). Which is also a very nice addition to filter out the amount of data in the responses of your web server.

What’s field selection?

Let us first make it clear what I mean with a field selection. Imagine that you have the following endpoint:

// > GET /api/products
[
  {
    "id": 1,
    "createdAt": "2024-18-11",
    "updatedAt": "2024-18-11",
    "code": "1",
    "price": {
      "amount": 100,
      "currency": "EUR"
    },
    "manufacturedBy": "myshop",
    "stock": 552,
    ...
  },
  ...
]
Enter fullscreen mode Exit fullscreen mode

Using partial responses, clients can filter the output with a fields query parameter, e.g.

// > GET /api/products?fields=code,price
[
  {
    "code": "1",
    "price": {
      "amount": 100,
      "currency": "EUR"
    }
  },
  ...
]
Enter fullscreen mode Exit fullscreen mode

Only the fields declared in the query parameter are returned. Reducing the payload size and saving bandwidth.

Introducing Milogo

I could not find any implementation available for this pattern, so I decided to create it myself, and that is how Milogo was born.
Milogo is a Gin middleware that processes API responses, filters out fields specified in the fields query parameter, and return only the requested data.

Some of the main features available:

// GET /products?fields=code,price(amount)
{
  "data": [
    {
      "code": 1,
      "price": {
        "amount": 100
      }
    },
    ...
  ],
  "_metadata": {
    ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Getting Started

As any gin middleware, Milogo is really easy to use and setup, you can follow the README in the github repository, but basically:

r := gin.Default()
r.Use(Milogo())
Enter fullscreen mode Exit fullscreen mode

is enough to add Milogo middleware to your api.

In the previous example Milogo is applied to every single endpoint, but it is also possible to apply only to a group of endpoints, or just to an specific endpoint, e.g.

group := r.Group("/products", milogo.Milogo())
group.GET("", productsHandler)
Enter fullscreen mode Exit fullscreen mode

In the previous example, only the /products group would have the Milogo’s middleware applied.

Conclusion

Milogo is designed to simplify REST API development and improve client-server interactions. Check out the GitHub repository for examples and documentation.

Feel free to reach out or contribute — let’s make REST APIs efficient together!

💖 💪 🙅 🚩
manuelarte
Manuel Doncel Martos

Posted on November 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related