http_server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/langgenius/dify-plugin-daemon/internal/server/controllers"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. )
  11. func (app *App) server(config *app.Config) func() {
  12. engine := gin.Default()
  13. engine.GET("/health/check", controllers.HealthCheck)
  14. engine.POST(
  15. "/plugin/tool/invoke",
  16. CheckingKey(config.PluginInnerApiKey),
  17. app.RedirectPluginInvoke(),
  18. controllers.InvokeTool,
  19. )
  20. engine.POST(
  21. "/plugin/tool/validate_credentials",
  22. CheckingKey(config.PluginInnerApiKey),
  23. app.RedirectPluginInvoke(),
  24. controllers.ValidateToolCredentials,
  25. )
  26. engine.POST(
  27. "/plugin/llm/invoke",
  28. CheckingKey(config.PluginInnerApiKey),
  29. app.RedirectPluginInvoke(),
  30. controllers.InvokeLLM,
  31. )
  32. engine.POST(
  33. "/plugin/text_embedding/invoke",
  34. CheckingKey(config.PluginInnerApiKey),
  35. app.RedirectPluginInvoke(),
  36. controllers.InvokeTextEmbedding,
  37. )
  38. engine.POST(
  39. "/plugin/rerank/invoke",
  40. CheckingKey(config.PluginInnerApiKey),
  41. app.RedirectPluginInvoke(),
  42. controllers.InvokeRerank,
  43. )
  44. engine.POST(
  45. "/plugin/tts/invoke",
  46. CheckingKey(config.PluginInnerApiKey),
  47. app.RedirectPluginInvoke(),
  48. controllers.InvokeTTS,
  49. )
  50. engine.POST(
  51. "/plugin/speech2text/invoke",
  52. CheckingKey(config.PluginInnerApiKey),
  53. app.RedirectPluginInvoke(),
  54. controllers.InvokeSpeech2Text,
  55. )
  56. engine.POST(
  57. "/plugin/moderation/invoke",
  58. CheckingKey(config.PluginInnerApiKey),
  59. app.RedirectPluginInvoke(),
  60. controllers.InvokeModeration,
  61. )
  62. engine.POST(
  63. "/plugin/model/validate_provider_credentials",
  64. CheckingKey(config.PluginInnerApiKey),
  65. app.RedirectPluginInvoke(),
  66. controllers.ValidateProviderCredentials,
  67. )
  68. engine.POST(
  69. "/plugin/model/validate_model_credentials",
  70. CheckingKey(config.PluginInnerApiKey),
  71. app.RedirectPluginInvoke(),
  72. controllers.ValidateModelCredentials,
  73. )
  74. if config.PluginRemoteInstallingEnabled {
  75. engine.POST(
  76. "/plugin/debugging/key",
  77. CheckingKey(config.PluginInnerApiKey),
  78. controllers.GetRemoteDebuggingKey,
  79. )
  80. }
  81. if config.PluginWebhookEnabled {
  82. engine.HEAD("/webhook/:hook_id/*path", app.Webhook())
  83. engine.POST("/webhook/:hook_id/*path", app.Webhook())
  84. engine.GET("/webhook/:hook_id/*path", app.Webhook())
  85. engine.PUT("/webhook/:hook_id/*path", app.Webhook())
  86. engine.DELETE("/webhook/:hook_id/*path", app.Webhook())
  87. engine.OPTIONS("/webhook/:hook_id/*path", app.Webhook())
  88. }
  89. srv := &http.Server{
  90. Addr: fmt.Sprintf(":%d", config.ServerPort),
  91. Handler: engine,
  92. }
  93. go func() {
  94. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  95. log.Panic("listen: %s\n", err)
  96. }
  97. }()
  98. return func() {
  99. if err := srv.Shutdown(context.Background()); err != nil {
  100. log.Panic("Server Shutdown: %s\n", err)
  101. }
  102. }
  103. }