context.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net"
  6. "net/http"
  7. "os"
  8. "path"
  9. "strings"
  10. )
  11. var (
  12. defaultBinder = &DefaultBinder{}
  13. )
  14. var (
  15. realIPHeaders = []string{
  16. "Cf-Connecting-Ip",
  17. "True-Client-IP",
  18. "X-Forwarded-For",
  19. "X-Real-Ip",
  20. }
  21. )
  22. type Context struct {
  23. ctx context.Context
  24. req *http.Request
  25. res http.ResponseWriter
  26. params map[string]string
  27. statusCode int
  28. }
  29. func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[string]string) {
  30. ctx.statusCode = http.StatusOK
  31. ctx.req, ctx.res, ctx.params = req, res, ps
  32. }
  33. func (ctx *Context) RealIp() string {
  34. var (
  35. pos int
  36. ipaddr string
  37. )
  38. for _, h := range realIPHeaders {
  39. if ipaddr = ctx.Request().Header.Get(h); ipaddr != "" {
  40. goto __end
  41. }
  42. }
  43. ipaddr, _, _ = net.SplitHostPort(ctx.Request().RemoteAddr)
  44. __end:
  45. if pos = strings.LastIndexByte(ipaddr, ','); pos > -1 {
  46. ipaddr = ipaddr[:pos]
  47. }
  48. return ipaddr
  49. }
  50. func (ctx *Context) Request() *http.Request {
  51. return ctx.req
  52. }
  53. func (ctx *Context) Response() http.ResponseWriter {
  54. return ctx.res
  55. }
  56. func (ctx *Context) Context() context.Context {
  57. if ctx.Request().Context() != nil {
  58. return ctx.Request().Context()
  59. }
  60. return ctx.ctx
  61. }
  62. func (ctx *Context) Bind(v any) (err error) {
  63. return defaultBinder.Bind(v, ctx.Request())
  64. }
  65. func (ctx *Context) Query(k string) string {
  66. return ctx.Request().FormValue(k)
  67. }
  68. func (ctx *Context) Param(k string) string {
  69. var (
  70. ok bool
  71. v string
  72. )
  73. if v, ok = ctx.params[k]; ok {
  74. return v
  75. }
  76. return ctx.Request().FormValue(k)
  77. }
  78. func (ctx *Context) json(res responsePayload) (err error) {
  79. ctx.Response().Header().Set("Content-Type", "application/json")
  80. encoder := json.NewEncoder(ctx.Response())
  81. if strings.HasPrefix(ctx.Request().Header.Get("User-Agent"), "curl") {
  82. encoder.SetIndent("", "\t")
  83. }
  84. return encoder.Encode(res)
  85. }
  86. func (ctx *Context) Success(v any) (err error) {
  87. return ctx.json(responsePayload{Data: v})
  88. }
  89. func (ctx *Context) Status(code int) {
  90. ctx.statusCode = code
  91. }
  92. func (ctx *Context) Error(code int, reason string) (err error) {
  93. return ctx.json(responsePayload{Code: code, Reason: reason})
  94. }
  95. func (ctx *Context) Redirect(url string, code int) {
  96. if code != http.StatusFound && code != http.StatusMovedPermanently {
  97. code = http.StatusMovedPermanently
  98. }
  99. http.Redirect(ctx.Response(), ctx.Request(), url, code)
  100. }
  101. func (ctx *Context) SetCookie(cookie *http.Cookie) {
  102. http.SetCookie(ctx.Response(), cookie)
  103. }
  104. func (ctx *Context) SendFile(filename string) (err error) {
  105. var (
  106. fi os.FileInfo
  107. fp *os.File
  108. )
  109. if fi, err = os.Stat(filename); err == nil {
  110. if fp, err = os.Open(filename); err == nil {
  111. http.ServeContent(ctx.Response(), ctx.Request(), path.Base(filename), fi.ModTime(), fp)
  112. err = fp.Close()
  113. }
  114. }
  115. return
  116. }