endpoint.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // check if plugin exists in current node
  39. if !app.cluster.IsPluginNoCurrentNode(endpoint.PluginID) {
  40. app.redirectPluginInvokeByPluginID(ctx, endpoint.PluginID)
  41. } else {
  42. service.Endpoint(ctx, &endpoint, path)
  43. }
  44. }