context.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package http
  2. import (
  3. "encoding/json"
  4. "net"
  5. "net/http"
  6. "os"
  7. "path"
  8. "strings"
  9. )
  10. var (
  11. binder = &DefaultBinder{}
  12. )
  13. type Context struct {
  14. request *http.Request
  15. response http.ResponseWriter
  16. params map[string]string
  17. }
  18. func (c *Context) Reset(r *http.Request, w http.ResponseWriter, params map[string]string) {
  19. c.request = r
  20. c.response = w
  21. c.params = params
  22. }
  23. func (c *Context) Request() *http.Request {
  24. return c.request
  25. }
  26. func (c *Context) Response() http.ResponseWriter {
  27. return c.response
  28. }
  29. func (c *Context) FormValue(name string) string {
  30. return c.request.FormValue(name)
  31. }
  32. func (c *Context) Bind(i interface{}) (err error) {
  33. return binder.Bind(i, c.Request())
  34. }
  35. func (c *Context) Success(val interface{}) (err error) {
  36. return c.JSON(&Response{Result: val})
  37. }
  38. func (c *Context) Redirect(url string, code int) {
  39. http.Redirect(c.Response(), c.Request(), url, code)
  40. }
  41. func (c *Context) SetCookie(cookie *http.Cookie) {
  42. http.SetCookie(c.Response(), cookie)
  43. }
  44. func (c *Context) GetRealIp() string {
  45. if ip := c.Request().Header.Get("X-Forwarded-For"); ip != "" {
  46. i := strings.IndexAny(ip, ",")
  47. if i > 0 {
  48. return strings.TrimSpace(ip[:i])
  49. }
  50. return ip
  51. }
  52. if ip := c.Request().Header.Get("X-Real-IP"); ip != "" {
  53. return ip
  54. }
  55. ra, _, _ := net.SplitHostPort(c.Request().RemoteAddr)
  56. return ra
  57. }
  58. func (c *Context) Error(code int, message string) (err error) {
  59. return c.JSON(&Response{Code: code, Message: message})
  60. }
  61. func (c *Context) JSON(v interface{}) (err error) {
  62. c.response.Header().Set("Content-Type", "application/json")
  63. enc := json.NewEncoder(c.response)
  64. if strings.Contains(c.request.Header.Get("User-Agent"), "curl") {
  65. enc.SetIndent("", "\t")
  66. }
  67. if err = enc.Encode(v); err != nil {
  68. c.response.WriteHeader(http.StatusBadGateway)
  69. }
  70. return
  71. }
  72. func (c *Context) SendFile(filename string) (err error) {
  73. var (
  74. fi os.FileInfo
  75. fp *os.File
  76. )
  77. if fi, err = os.Stat(filename); err == nil {
  78. if fp, err = os.Open(filename); err == nil {
  79. http.ServeContent(c.Response(), c.Request(), path.Base(filename), fi.ModTime(), fp)
  80. err = fp.Close()
  81. }
  82. }
  83. return
  84. }
  85. func (c *Context) SetParam(name string, value string) {
  86. if c.params == nil {
  87. c.params = make(map[string]string)
  88. }
  89. c.params[name] = value
  90. }
  91. func (c *Context) ParamValue(name string) string {
  92. if c.params != nil {
  93. return c.params[name]
  94. }
  95. return ""
  96. }