base.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package controllers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/langgenius/dify-plugin-daemon/internal/server/constants"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/validators"
  8. )
  9. func BindRequest[T any](r *gin.Context, success func(T)) {
  10. var request T
  11. err := r.ShouldBindUri(&request)
  12. if err != nil {
  13. resp := entities.NewErrorResponse(-400, err.Error())
  14. r.JSON(400, resp)
  15. return
  16. }
  17. // validate, we have customized some validators which are not supported by gin binding
  18. if err := validators.GlobalEntitiesValidator.Struct(request); err != nil {
  19. resp := entities.NewErrorResponse(-400, err.Error())
  20. r.JSON(400, resp)
  21. return
  22. }
  23. success(request)
  24. }
  25. func BindRequestWithPluginUniqueIdentifier[T any](r *gin.Context, success func(
  26. T, plugin_entities.PluginUniqueIdentifier,
  27. )) {
  28. BindRequest(r, func(req T) {
  29. plugin_unique_identifier := r.GetHeader(constants.X_PLUGIN_IDENTIFIER)
  30. if plugin_unique_identifier == "" {
  31. resp := entities.NewErrorResponse(-400, "Plugin unique identifier is required")
  32. r.JSON(400, resp)
  33. return
  34. }
  35. identifier, err := plugin_entities.NewPluginUniqueIdentifier(plugin_unique_identifier)
  36. if err != nil {
  37. resp := entities.NewErrorResponse(-400, err.Error())
  38. r.JSON(400, resp)
  39. return
  40. }
  41. success(req, identifier)
  42. })
  43. }