header.go 506 B

123456789101112131415161718192021222324
  1. package http
  2. import (
  3. "fmt"
  4. bm "go-common/library/net/http/blademaster"
  5. "go-common/library/net/trace"
  6. )
  7. // WrapHeader 为返回头添加自定义字段
  8. func WrapHeader(ctx *bm.Context) {
  9. // Traceid
  10. tracer, _ := trace.FromContext(ctx.Context)
  11. traceid := fmt.Sprintf("%s", tracer)
  12. ctx.Writer.Header().Set("traceid", traceid)
  13. // Sessionid
  14. sid := ctx.Request.Header.Get("SessionID")
  15. if sid == "" {
  16. sid = SessionID(ctx)
  17. }
  18. ctx.Set("SessionID", sid)
  19. ctx.Writer.Header().Set("SessionID", sid)
  20. }