context.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. type Context struct {
  15. ctx context.Context
  16. req *http.Request
  17. res http.ResponseWriter
  18. params map[string]string
  19. statusCode int
  20. }
  21. func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[string]string) {
  22. ctx.statusCode = http.StatusOK
  23. ctx.req, ctx.res, ctx.params = req, res, ps
  24. }
  25. func (ctx *Context) RealIp() string {
  26. if ip := ctx.Request().Header.Get("X-Forwarded-For"); ip != "" {
  27. i := strings.IndexAny(ip, ",")
  28. if i > 0 {
  29. return strings.TrimSpace(ip[:i])
  30. }
  31. return ip
  32. }
  33. if ip := ctx.Request().Header.Get("X-Real-IP"); ip != "" {
  34. return ip
  35. }
  36. ra, _, _ := net.SplitHostPort(ctx.Request().RemoteAddr)
  37. return ra
  38. }
  39. func (ctx *Context) Request() *http.Request {
  40. return ctx.req
  41. }
  42. func (ctx *Context) Response() http.ResponseWriter {
  43. return ctx.res
  44. }
  45. func (ctx *Context) Context() context.Context {
  46. if ctx.Request().Context() != nil {
  47. return ctx.Request().Context()
  48. }
  49. return ctx.ctx
  50. }
  51. func (ctx *Context) Bind(v any) (err error) {
  52. return defaultBinder.Bind(v, ctx.Request())
  53. }
  54. func (ctx *Context) Query(k string) string {
  55. return ctx.Request().FormValue(k)
  56. }
  57. func (ctx *Context) Param(k string) string {
  58. var (
  59. ok bool
  60. v string
  61. )
  62. if v, ok = ctx.params[k]; ok {
  63. return v
  64. }
  65. return ctx.Request().FormValue(k)
  66. }
  67. func (ctx *Context) send(res responsePayload) (err error) {
  68. ctx.Response().Header().Set("Content-Type", "application/json")
  69. encoder := json.NewEncoder(ctx.Response())
  70. if strings.HasPrefix(ctx.Request().Header.Get("User-Agent"), "curl") {
  71. encoder.SetIndent("", "\t")
  72. }
  73. return encoder.Encode(res)
  74. }
  75. func (ctx *Context) Success(v any) (err error) {
  76. return ctx.send(responsePayload{Data: v})
  77. }
  78. func (ctx *Context) Status(code int) {
  79. ctx.statusCode = code
  80. }
  81. func (ctx *Context) Error(code int, reason string) (err error) {
  82. return ctx.send(responsePayload{Code: code, Reason: reason})
  83. }
  84. func (ctx *Context) Redirect(url string, code int) {
  85. if code != http.StatusFound && code != http.StatusMovedPermanently {
  86. code = http.StatusMovedPermanently
  87. }
  88. http.Redirect(ctx.Response(), ctx.Request(), url, code)
  89. }
  90. func (ctx *Context) SetCookie(cookie *http.Cookie) {
  91. http.SetCookie(ctx.Response(), cookie)
  92. }
  93. func (ctx *Context) SendFile(filename string) (err error) {
  94. var (
  95. fi os.FileInfo
  96. fp *os.File
  97. )
  98. if fi, err = os.Stat(filename); err == nil {
  99. if fp, err = os.Open(filename); err == nil {
  100. http.ServeContent(ctx.Response(), ctx.Request(), path.Base(filename), fi.ModTime(), fp)
  101. err = fp.Close()
  102. }
  103. }
  104. return
  105. }