middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package server
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/gin-gonic/gin"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  7. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  9. )
  10. func CheckingKey(key string) gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. // get header X-Api-Key
  13. if c.GetHeader("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, err := parser.UnmarshalJsonBytes[plugin_entities.InvokePluginPluginIdentity](raw)
  43. if err != nil {
  44. ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request"})
  45. return
  46. }
  47. plugin_id := parser.MarshalPluginIdentity(identity.PluginName, identity.PluginVersion)
  48. // check if plugin in current node
  49. if !app.cluster.IsPluginNoCurrentNode(
  50. plugin_id,
  51. ) {
  52. // try find the correct node
  53. nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_id)
  54. if err != nil {
  55. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  56. log.Error("fetch plugin available nodes failed: %s", err.Error())
  57. return
  58. } else if len(nodes) == 0 {
  59. ctx.AbortWithStatusJSON(404, gin.H{"error": "No available node"})
  60. log.Error("no available node")
  61. return
  62. }
  63. // redirect to the correct node
  64. node_id := nodes[0]
  65. status_code, header, body, err := app.cluster.RedirectRequest(node_id, ctx.Request)
  66. if err != nil {
  67. log.Error("redirect request failed: %s", err.Error())
  68. ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
  69. return
  70. }
  71. // set status code
  72. ctx.Writer.WriteHeader(status_code)
  73. // set header
  74. for key, values := range header {
  75. for _, value := range values {
  76. ctx.Writer.Header().Set(key, value)
  77. }
  78. }
  79. for {
  80. buf := make([]byte, 1024)
  81. n, err := body.Read(buf)
  82. if err != nil && err != io.EOF {
  83. break
  84. } else if err != nil {
  85. ctx.Writer.Write(buf[:n])
  86. break
  87. }
  88. if n > 0 {
  89. ctx.Writer.Write(buf[:n])
  90. }
  91. }
  92. ctx.Abort()
  93. return
  94. } else {
  95. ctx.Next()
  96. }
  97. }
  98. }