middleware.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package server
  2. import (
  3. "errors"
  4. "io"
  5. "github.com/gin-gonic/gin"
  6. "github.com/langgenius/dify-plugin-daemon/internal/db"
  7. "github.com/langgenius/dify-plugin-daemon/internal/server/constants"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/exception"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  12. )
  13. func CheckingKey(key string) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. // get header X-Api-Key
  16. if c.GetHeader(constants.X_API_KEY) != key {
  17. c.AbortWithStatusJSON(401, exception.UnauthorizedError().ToResponse())
  18. return
  19. }
  20. c.Next()
  21. }
  22. }
  23. func (app *App) FetchPluginInstallation() gin.HandlerFunc {
  24. return func(ctx *gin.Context) {
  25. pluginId := ctx.Request.Header.Get(constants.X_PLUGIN_ID)
  26. if pluginId == "" {
  27. ctx.AbortWithStatusJSON(400, exception.BadRequestError(errors.New("plugin_id is required")).ToResponse())
  28. return
  29. }
  30. tenantId := ctx.Param("tenant_id")
  31. if tenantId == "" {
  32. ctx.AbortWithStatusJSON(400, exception.BadRequestError(errors.New("tenant_id is required")).ToResponse())
  33. return
  34. }
  35. // fetch plugin installation
  36. installation, err := db.GetOne[models.PluginInstallation](
  37. db.Equal("tenant_id", tenantId),
  38. db.Equal("plugin_id", pluginId),
  39. )
  40. if err == db.ErrDatabaseNotFound {
  41. ctx.AbortWithStatusJSON(404, exception.ErrPluginNotFound().ToResponse())
  42. return
  43. }
  44. if err != nil {
  45. ctx.AbortWithStatusJSON(500, exception.InternalServerError(err).ToResponse())
  46. return
  47. }
  48. identity, err := plugin_entities.NewPluginUniqueIdentifier(installation.PluginUniqueIdentifier)
  49. if err != nil {
  50. ctx.AbortWithStatusJSON(400, exception.PluginUniqueIdentifierError(err).ToResponse())
  51. return
  52. }
  53. ctx.Set(constants.CONTEXT_KEY_PLUGIN_INSTALLATION, installation)
  54. ctx.Set(constants.CONTEXT_KEY_PLUGIN_UNIQUE_IDENTIFIER, identity)
  55. ctx.Next()
  56. }
  57. }
  58. // RedirectPluginInvoke redirects the request to the correct cluster node
  59. func (app *App) RedirectPluginInvoke() gin.HandlerFunc {
  60. return func(ctx *gin.Context) {
  61. // get plugin unique identifier
  62. identityAny, ok := ctx.Get(constants.CONTEXT_KEY_PLUGIN_UNIQUE_IDENTIFIER)
  63. if !ok {
  64. ctx.AbortWithStatusJSON(
  65. 500,
  66. exception.InternalServerError(errors.New("plugin unique identifier not found")).ToResponse(),
  67. )
  68. return
  69. }
  70. identity, ok := identityAny.(plugin_entities.PluginUniqueIdentifier)
  71. if !ok {
  72. ctx.AbortWithStatusJSON(
  73. 500,
  74. exception.InternalServerError(errors.New("failed to parse plugin unique identifier")).ToResponse(),
  75. )
  76. return
  77. }
  78. // check if plugin in current node
  79. if ok, originalError := app.cluster.IsPluginOnCurrentNode(identity); !ok {
  80. app.redirectPluginInvokeByPluginIdentifier(ctx, identity, originalError)
  81. ctx.Abort()
  82. } else {
  83. ctx.Next()
  84. }
  85. }
  86. }
  87. func (app *App) redirectPluginInvokeByPluginIdentifier(
  88. ctx *gin.Context,
  89. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  90. originalError error,
  91. ) {
  92. // try find the correct node
  93. nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_unique_identifier.String())
  94. if err != nil {
  95. ctx.AbortWithStatusJSON(
  96. 500,
  97. exception.InternalServerError(
  98. errors.New("failed to fetch plugin available nodes, "+originalError.Error()+", "+err.Error()),
  99. ).ToResponse(),
  100. )
  101. return
  102. } else if len(nodes) == 0 {
  103. ctx.AbortWithStatusJSON(
  104. 404,
  105. exception.InternalServerError(
  106. errors.New("no available node, "+originalError.Error()),
  107. ).ToResponse(),
  108. )
  109. return
  110. }
  111. // redirect to the correct node
  112. nodeId := nodes[0]
  113. statusCode, header, body, err := app.cluster.RedirectRequest(nodeId, ctx.Request)
  114. if err != nil {
  115. log.Error("redirect request failed: %s", err.Error())
  116. ctx.AbortWithStatusJSON(
  117. 500,
  118. exception.InternalServerError(errors.New("redirect request failed: "+err.Error())).ToResponse(),
  119. )
  120. return
  121. }
  122. // set status code
  123. ctx.Writer.WriteHeader(statusCode)
  124. // set header
  125. for key, values := range header {
  126. for _, value := range values {
  127. ctx.Writer.Header().Set(key, value)
  128. }
  129. }
  130. for {
  131. buf := make([]byte, 1024)
  132. n, err := body.Read(buf)
  133. if err != nil && err != io.EOF {
  134. break
  135. } else if err != nil {
  136. ctx.Writer.Write(buf[:n])
  137. break
  138. }
  139. if n > 0 {
  140. ctx.Writer.Write(buf[:n])
  141. }
  142. }
  143. }
  144. func (app *App) InitClusterID() gin.HandlerFunc {
  145. return func(ctx *gin.Context) {
  146. ctx.Set(constants.CONTEXT_KEY_CLUSTER_ID, app.cluster.ID())
  147. ctx.Next()
  148. }
  149. }