douhui4699 2014-08-13 17:07
浏览 55
已采纳

Golang中的服务器头中间件

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
  • 写回答

4条回答 默认 最新

  • dongmangzong8006 2014-08-14 00:45
    关注

    You did not use the right method for that ..

    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) WriteHeader(code int) {
        if w.Header().Get("Server") == "" {
            w.Header().Add("Server", w.ServerInfo)
        }
    
        w.ResponseWriter.WriteHeader(code)
    }
    
    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")
    }
    

    Here is the 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
    < Server: Some-Play-Server
    < Date: Thu, 14 Aug 2014 00:41:27 GMT
    < Content-Length: 2
    < 
    OK
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1