I'm trying to set Server Header on every Response. I'm trying to achieve this, using a Middleware for Gin. However, this does not set the Header, for some reason. I've tried to debug this so far, and I could not understand why this should not work. Probably I'm missing something here.
Here is the code
package main
import "fmt"
import "github.com/gin-gonic/gin"
const SERVER_INFO = "Some-Play-Server"
type ServerHeader struct {
gin.ResponseWriter
ServerInfo string
}
func (w *ServerHeader) Write(data []byte) (int, error) {
if w.Header().Get("Server") == "" {
w.Header().Add("Server", w.ServerInfo)
}
return w.ResponseWriter.Write(data)
}
func InitServerHeader() gin.HandlerFunc {
return func(c *gin.Context) {
writer := &ServerHeader{c.Writer, SERVER_INFO}
c.Writer = writer
c.Next()
}
}
func main() {
mux := gin.Default()
mux.Use(InitServerHeader())
mux.GET("/", func(c *gin.Context) {
c.String(200, "OK")
})
fmt.Println("Server Listening on 0.0.0.0:8080")
mux.Run(":8080")
}
And, here is the Test Output
❯ curl -v http://localhost:8080/
* About to connect() to localhost port 8080 (#0)
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Wed, 13 Aug 2014 16:54:21 GMT
< Content-Length: 2
<
OK