gin.go 2.6 KB

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