endpoint.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/hex"
  6. "sync/atomic"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  10. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
  11. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  12. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  13. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  14. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
  15. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  16. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  17. )
  18. func Endpoint(
  19. ctx *gin.Context,
  20. endpoint *models.Endpoint,
  21. path string,
  22. ) {
  23. req := ctx.Request.Clone(context.Background())
  24. req.URL.Path = path
  25. var buffer bytes.Buffer
  26. err := req.Write(&buffer)
  27. if err != nil {
  28. ctx.JSON(500, gin.H{"error": err.Error()})
  29. }
  30. // fetch plugin
  31. manager := plugin_manager.GetGlobalPluginManager()
  32. runtime := manager.Get(endpoint.PluginID)
  33. if runtime == nil {
  34. ctx.JSON(404, gin.H{"error": "plugin not found"})
  35. return
  36. }
  37. // fetch endpoint declaration
  38. endpoint_declaration := runtime.Configuration().Endpoint
  39. if endpoint_declaration == nil {
  40. ctx.JSON(404, gin.H{"error": "endpoint declaration not found"})
  41. return
  42. }
  43. // decrypt settings
  44. settings, err := dify_invocation.InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
  45. BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
  46. TenantId: endpoint.TenantID,
  47. UserId: "",
  48. Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
  49. },
  50. InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
  51. Opt: dify_invocation.ENCRYPT_OPT_DECRYPT,
  52. Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
  53. Identity: endpoint.ID,
  54. Data: endpoint.GetSettings(),
  55. Config: endpoint_declaration.Settings,
  56. },
  57. })
  58. if err != nil {
  59. ctx.JSON(500, gin.H{"error": "failed to decrypt data"})
  60. return
  61. }
  62. session := session_manager.NewSession(
  63. endpoint.TenantID,
  64. "",
  65. endpoint.PluginID,
  66. ctx.GetString("cluster_id"),
  67. access_types.PLUGIN_ACCESS_TYPE_Endpoint,
  68. access_types.PLUGIN_ACCESS_ACTION_INVOKE_ENDPOINT,
  69. runtime.Configuration(),
  70. )
  71. defer session.Close()
  72. session.BindRuntime(runtime)
  73. status_code, headers, response, err := plugin_daemon.InvokeEndpoint(
  74. session, &requests.RequestInvokeEndpoint{
  75. RawHttpRequest: hex.EncodeToString(buffer.Bytes()),
  76. Settings: settings,
  77. },
  78. )
  79. if err != nil {
  80. ctx.JSON(500, gin.H{"error": err.Error()})
  81. return
  82. }
  83. defer response.Close()
  84. done := make(chan bool)
  85. closed := new(int32)
  86. ctx.Status(status_code)
  87. for k, v := range *headers {
  88. if len(v) > 0 {
  89. ctx.Writer.Header().Set(k, v[0])
  90. }
  91. }
  92. close := func() {
  93. if atomic.CompareAndSwapInt32(closed, 0, 1) {
  94. close(done)
  95. }
  96. }
  97. defer close()
  98. routine.Submit(func() {
  99. defer close()
  100. for response.Next() {
  101. chunk, err := response.Read()
  102. if err != nil {
  103. ctx.JSON(500, gin.H{"error": err.Error()})
  104. return
  105. }
  106. ctx.Writer.Write(chunk)
  107. ctx.Writer.Flush()
  108. }
  109. })
  110. select {
  111. case <-ctx.Writer.CloseNotify():
  112. case <-done:
  113. case <-time.After(30 * time.Second):
  114. ctx.JSON(500, gin.H{"error": "killed by timeout"})
  115. }
  116. }