How to add default header to response in golang gin?

Viewed 1981

How to add default header to response returned in Gin golang? I wanted to add Cache-Control: public, max-age=604800, immutable to the every response that I return.

1 Answers

I have solved the above question with following code

r := gin.New()

r.Use(func() gin.HandlerFunc {
        return func(c *gin.Context) {
            c.Writer.Header().Set("Cache-Control", "public, max-age=604800, immutable")
        }
    }()
)

However, it is not really good idea to add the given header to every request. It is better to add the header to cdn or static items. It can be achieved with NGINX or Traefik.

Related