base.go 832 B

1234567891011121314151617181920212223242526272829303132333435
  1. package controllers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/validators"
  6. )
  7. func BindRequest[T any](r *gin.Context, success func(T)) {
  8. var request T
  9. var err error
  10. context_type := r.GetHeader("Content-Type")
  11. if context_type == "application/json" {
  12. err = r.ShouldBindJSON(&request)
  13. } else {
  14. err = r.ShouldBind(&request)
  15. }
  16. if err != nil {
  17. resp := entities.NewErrorResponse(-400, err.Error())
  18. r.JSON(400, resp)
  19. return
  20. }
  21. // validate, we have customized some validators which are not supported by gin binding
  22. if err := validators.GlobalEntitiesValidator.Struct(request); err != nil {
  23. resp := entities.NewErrorResponse(-400, err.Error())
  24. r.JSON(400, resp)
  25. return
  26. }
  27. success(request)
  28. }