gin.go 2.5 KB

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