utils.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package middleware
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/labring/aiproxy/core/common"
  6. "github.com/labring/aiproxy/core/relay/mode"
  7. relaymodel "github.com/labring/aiproxy/core/relay/model"
  8. )
  9. func AbortLogWithMessageWithMode(
  10. m mode.Mode,
  11. c *gin.Context,
  12. statusCode int,
  13. message string,
  14. typ ...string,
  15. ) {
  16. common.GetLogger(c).Error(message)
  17. AbortWithMessageWithMode(m, c, statusCode, message, typ...)
  18. }
  19. func AbortWithMessageWithMode(
  20. m mode.Mode,
  21. c *gin.Context,
  22. statusCode int,
  23. message string,
  24. typ ...string,
  25. ) {
  26. c.JSON(statusCode,
  27. relaymodel.WrapperErrorWithMessage(m, statusCode, message, typ...),
  28. )
  29. c.Abort()
  30. }
  31. func AbortLogWithMessage(c *gin.Context, statusCode int, message string, typ ...string) {
  32. common.GetLogger(c).Error(message)
  33. AbortWithMessage(c, statusCode, message, typ...)
  34. }
  35. func AbortWithMessage(c *gin.Context, statusCode int, message string, typ ...string) {
  36. c.JSON(statusCode,
  37. relaymodel.WrapperErrorWithMessage(GetMode(c), statusCode, message, typ...),
  38. )
  39. c.Abort()
  40. }
  41. func GetMode(c *gin.Context) mode.Mode {
  42. m, exists := c.Get(Mode)
  43. if !exists {
  44. return mode.Unknown
  45. }
  46. v, ok := m.(mode.Mode)
  47. if !ok {
  48. panic(fmt.Sprintf("mode type error: %T, %v", v, v))
  49. }
  50. return v
  51. }