我正在尝试在每个响应上设置服务器标头。我正在尝试使用Gin的中间件来实现这一点。但是,由于某种原因,这不会设置标题。到目前为止,我已经尝试对此进行调试,但我不明白为什么这不起作用。可能我在这里遗漏了一些东西。
这是代码
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")
}
而且,这是测试输出
❯ 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
相关分类