middleware.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package server
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/gin-gonic/gin"
  6. "github.com/langgenius/dify-plugin-daemon/internal/server/constants"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  9. )
  10. func CheckingKey(key string) gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. // get header X-Api-Key
  13. if c.GetHeader(constants.X_API_KEY) != key {
  14. c.JSON(401, gin.H{"error": "Unauthorized"})
  15. c.Abort()
  16. return
  17. }
  18. c.Next()
  19. }
  20. }
  21. type ginContextReader struct {
  22. reader *bytes.Reader
  23. }
  24. func (g *ginContextReader) Read(p []byte) (n int, err error) {
  25. return g.reader.Read(p)
  26. }
  27. func (g *ginContextReader) Close() error {
  28. return nil
  29. }
  30. // RedirectPluginInvoke redirects the request to the correct cluster node
  31. func (app *App) RedirectPluginInvoke() gin.HandlerFunc {
  32. return func(ctx *gin.Context) {
  33. // get plugin identity
  34. raw, err := ctx.GetRawData()
  35. if err != nil {
  36. ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request"})
  37. return
  38. }
  39. ctx.Request.Body = &ginContextReader{
  40. reader: bytes.NewReader(raw),
  41. }
  42. identity := plugin_entities.PluginUniqueIdentifier(ctx.Request.Header.Get(constants.X_PLUGIN_IDENTIFIER))
  43. if identity == "" {
  44. ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request"})
  45. return
  46. }
  47. // check if plugin in current node
  48. if !app.cluster.IsPluginNoCurrentNode(
  49. identity,
  50. ) {
  51. app.redirectPluginInvokeByPluginIdentifier(ctx, identity)
  52. ctx.Abort()
  53. } else {
  54. ctx.Next()
  55. }
  56. }
  57. }
  58. func (app *App) redirectPluginInvokeByPluginIdentifier(
  59. ctx *gin.Context,
  60. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  61. ) {
  62. // try find the correct node
  63. nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_unique_identifier.String())
  64. if err != nil {
  65. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  66. log.Error("fetch plugin available nodes failed: %s", err.Error())
  67. return
  68. } else if len(nodes) == 0 {
  69. ctx.AbortWithStatusJSON(404, gin.H{"error": "No available node"})
  70. log.Error("no available node")
  71. return
  72. }
  73. // redirect to the correct node
  74. node_id := nodes[0]
  75. status_code, header, body, err := app.cluster.RedirectRequest(node_id, ctx.Request)
  76. if err != nil {
  77. log.Error("redirect request failed: %s", err.Error())
  78. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  79. return
  80. }
  81. // set status code
  82. ctx.Writer.WriteHeader(status_code)
  83. // set header
  84. for key, values := range header {
  85. for _, value := range values {
  86. ctx.Writer.Header().Set(key, value)
  87. }
  88. }
  89. for {
  90. buf := make([]byte, 1024)
  91. n, err := body.Read(buf)
  92. if err != nil && err != io.EOF {
  93. break
  94. } else if err != nil {
  95. ctx.Writer.Write(buf[:n])
  96. break
  97. }
  98. if n > 0 {
  99. ctx.Writer.Write(buf[:n])
  100. }
  101. }
  102. }
  103. func (app *App) InitClusterID() gin.HandlerFunc {
  104. return func(ctx *gin.Context) {
  105. ctx.Set("cluster_id", app.cluster.ID())
  106. ctx.Next()
  107. }
  108. }