How it works...

Middlewares can be used to perform simple operations such as logging, metric collection, and analytics. Middlewares can also be used to dynamically populate variables on each request. This can be done, for example, to collect an X-header from the request to set an ID or generate an ID, like we did in this recipe. Another ID strategy might be to generate a Universal Unique Identifier (UUID) for every request—this allows us to easily correlate log messages together and trace your request across different applications if multiple microservices are involved in building the response.

When working with context values, it's important to consider the order of your middlewares. Typically, it's better to not make middlewares reliant on one another. For example, in this recipe, it would probably be better to generate the UUID in the logging middleware itself. However, this recipe should serve as a guide for layering middlewares and initializing them in main.go.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.105.105