gin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package common
  2. import (
  3. "bytes"
  4. "io"
  5. "mime/multipart"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/QuantumNous/new-api/constant"
  10. "github.com/gin-gonic/gin"
  11. )
  12. const KeyRequestBody = "key_request_body"
  13. func GetRequestBody(c *gin.Context) ([]byte, error) {
  14. requestBody, _ := c.Get(KeyRequestBody)
  15. if requestBody != nil {
  16. return requestBody.([]byte), nil
  17. }
  18. requestBody, err := io.ReadAll(c.Request.Body)
  19. if err != nil {
  20. return nil, err
  21. }
  22. _ = c.Request.Body.Close()
  23. c.Set(KeyRequestBody, requestBody)
  24. return requestBody.([]byte), nil
  25. }
  26. func UnmarshalBodyReusable(c *gin.Context, v any) error {
  27. requestBody, err := GetRequestBody(c)
  28. if err != nil {
  29. return err
  30. }
  31. //if DebugEnabled {
  32. // println("UnmarshalBodyReusable request body:", string(requestBody))
  33. //}
  34. contentType := c.Request.Header.Get("Content-Type")
  35. if strings.HasPrefix(contentType, "application/json") {
  36. err = Unmarshal(requestBody, &v)
  37. } else {
  38. // skip for now
  39. // TODO: someday non json request have variant model, we will need to implementation this
  40. }
  41. if err != nil {
  42. return err
  43. }
  44. // Reset request body
  45. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  46. return nil
  47. }
  48. func SetContextKey(c *gin.Context, key constant.ContextKey, value any) {
  49. c.Set(string(key), value)
  50. }
  51. func GetContextKey(c *gin.Context, key constant.ContextKey) (any, bool) {
  52. return c.Get(string(key))
  53. }
  54. func GetContextKeyString(c *gin.Context, key constant.ContextKey) string {
  55. return c.GetString(string(key))
  56. }
  57. func GetContextKeyInt(c *gin.Context, key constant.ContextKey) int {
  58. return c.GetInt(string(key))
  59. }
  60. func GetContextKeyBool(c *gin.Context, key constant.ContextKey) bool {
  61. return c.GetBool(string(key))
  62. }
  63. func GetContextKeyStringSlice(c *gin.Context, key constant.ContextKey) []string {
  64. return c.GetStringSlice(string(key))
  65. }
  66. func GetContextKeyStringMap(c *gin.Context, key constant.ContextKey) map[string]any {
  67. return c.GetStringMap(string(key))
  68. }
  69. func GetContextKeyTime(c *gin.Context, key constant.ContextKey) time.Time {
  70. return c.GetTime(string(key))
  71. }
  72. func GetContextKeyType[T any](c *gin.Context, key constant.ContextKey) (T, bool) {
  73. if value, ok := c.Get(string(key)); ok {
  74. if v, ok := value.(T); ok {
  75. return v, true
  76. }
  77. }
  78. var t T
  79. return t, false
  80. }
  81. func ApiError(c *gin.Context, err error) {
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": false,
  84. "message": err.Error(),
  85. })
  86. }
  87. func ApiErrorMsg(c *gin.Context, msg string) {
  88. c.JSON(http.StatusOK, gin.H{
  89. "success": false,
  90. "message": msg,
  91. })
  92. }
  93. func ApiSuccess(c *gin.Context, data any) {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": true,
  96. "message": "",
  97. "data": data,
  98. })
  99. }
  100. func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
  101. requestBody, err := GetRequestBody(c)
  102. if err != nil {
  103. return nil, err
  104. }
  105. contentType := c.Request.Header.Get("Content-Type")
  106. boundary := ""
  107. if idx := strings.Index(contentType, "boundary="); idx != -1 {
  108. boundary = contentType[idx+9:]
  109. }
  110. reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
  111. form, err := reader.ReadForm(32 << 20) // 32 MB max memory
  112. if err != nil {
  113. return nil, err
  114. }
  115. // Reset request body
  116. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  117. return form, nil
  118. }