endpoint.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/langgenius/dify-plugin-daemon/internal/db"
  5. "github.com/langgenius/dify-plugin-daemon/internal/service"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  7. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  8. )
  9. // DifyPlugin supports register and use endpoint to improve the plugin's functionality
  10. // you can use it to do some magics, looking forward to your imagination, Ciallo~(∠·ω< )⌒
  11. // - Yeuoly
  12. // EndpointHandler is a function type that can be used to handle endpoint requests
  13. type EndpointHandler func(ctx *gin.Context, hook_id string, path string)
  14. func (app *App) Endpoint() func(c *gin.Context) {
  15. return func(c *gin.Context) {
  16. hook_id := c.Param("hook_id")
  17. path := c.Param("path")
  18. if app.endpoint_handler != nil {
  19. app.endpoint_handler(c, hook_id, path)
  20. } else {
  21. app.EndpointHandler(c, hook_id, path)
  22. }
  23. }
  24. }
  25. func (app *App) EndpointHandler(ctx *gin.Context, hook_id string, path string) {
  26. endpoint, err := db.GetOne[models.Endpoint](
  27. db.Equal("hook_id", hook_id),
  28. )
  29. if err == db.ErrDatabaseNotFound {
  30. ctx.JSON(404, gin.H{"error": "endpoint not found"})
  31. return
  32. }
  33. if err != nil {
  34. log.Error("get endpoint error %v", err)
  35. ctx.JSON(500, gin.H{"error": "internal server error"})
  36. return
  37. }
  38. // get plugin installation
  39. plugin_installation, err := db.GetOne[models.PluginInstallation](
  40. db.Equal("plugin_id", endpoint.PluginID),
  41. db.Equal("tenant_id", endpoint.TenantID),
  42. )
  43. if err != nil {
  44. ctx.JSON(404, gin.H{"error": "plugin installation not found"})
  45. return
  46. }
  47. // check if plugin exists in current node
  48. if !app.cluster.IsPluginNoCurrentNode(plugin_installation.PluginIdentity) {
  49. app.redirectPluginInvokeByPluginID(ctx, plugin_installation.PluginIdentity)
  50. } else {
  51. service.Endpoint(ctx, &endpoint, &plugin_installation, path)
  52. }
  53. }