context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. s string
  36. pos int
  37. ipaddr string
  38. )
  39. for _, h := range realIPHeaders {
  40. if ipaddr = ctx.Request().Header.Get(h); ipaddr != "" {
  41. goto __end
  42. }
  43. }
  44. ipaddr, _, _ = net.SplitHostPort(ctx.Request().RemoteAddr)
  45. __end:
  46. for {
  47. if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
  48. s = strings.TrimSpace(ipaddr[:pos])
  49. if netip := net.ParseIP(s); netip != nil && !netip.IsPrivate() {
  50. return s
  51. }
  52. ipaddr = ipaddr[pos+1:]
  53. } else {
  54. break
  55. }
  56. }
  57. return strings.TrimSpace(ipaddr)
  58. }
  59. func (ctx *Context) Request() *http.Request {
  60. return ctx.req
  61. }
  62. func (ctx *Context) Response() http.ResponseWriter {
  63. return ctx.res
  64. }
  65. func (ctx *Context) Context() context.Context {
  66. if ctx.Request().Context() != nil {
  67. return ctx.Request().Context()
  68. }
  69. return ctx.ctx
  70. }
  71. func (ctx *Context) Bind(v any) (err error) {
  72. return defaultBinder.Bind(v, ctx.Request())
  73. }
  74. func (ctx *Context) Query(k string) string {
  75. return ctx.Request().FormValue(k)
  76. }
  77. func (ctx *Context) Param(k string) string {
  78. var (
  79. ok bool
  80. v string
  81. )
  82. if v, ok = ctx.params[k]; ok {
  83. return v
  84. }
  85. return ctx.Request().FormValue(k)
  86. }
  87. func (ctx *Context) json(res responsePayload) (err error) {
  88. ctx.Response().Header().Set("Content-Type", "application/json")
  89. encoder := json.NewEncoder(ctx.Response())
  90. if strings.HasPrefix(ctx.Request().Header.Get("User-Agent"), "curl") {
  91. encoder.SetIndent("", "\t")
  92. }
  93. return encoder.Encode(res)
  94. }
  95. func (ctx *Context) Success(v any) (err error) {
  96. return ctx.json(responsePayload{Data: v})
  97. }
  98. func (ctx *Context) Status(code int) {
  99. ctx.statusCode = code
  100. }
  101. func (ctx *Context) Error(code int, reason string) (err error) {
  102. return ctx.json(responsePayload{Code: code, Reason: reason})
  103. }
  104. func (ctx *Context) Redirect(url string, code int) {
  105. if code != http.StatusFound && code != http.StatusMovedPermanently {
  106. code = http.StatusFound
  107. }
  108. http.Redirect(ctx.Response(), ctx.Request(), url, code)
  109. }
  110. func (ctx *Context) SetCookie(cookie *http.Cookie) {
  111. http.SetCookie(ctx.Response(), cookie)
  112. }
  113. func (ctx *Context) SendFile(filename string) (err error) {
  114. var (
  115. fi os.FileInfo
  116. fp *os.File
  117. )
  118. if fi, err = os.Stat(filename); err == nil {
  119. if fp, err = os.Open(filename); err == nil {
  120. http.ServeContent(ctx.Response(), ctx.Request(), path.Base(filename), fi.ModTime(), fp)
  121. err = fp.Close()
  122. }
  123. }
  124. return
  125. }