http_server.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. app.pluginInvokeGroup(engine.Group("/plugin"), config)
  15. app.remoteDebuggingGroup(engine.Group("/plugin/debugging"), config)
  16. app.webhookGroup(engine.Group("/webhook"), config)
  17. srv := &http.Server{
  18. Addr: fmt.Sprintf(":%d", config.ServerPort),
  19. Handler: engine,
  20. }
  21. go func() {
  22. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  23. log.Panic("listen: %s\n", err)
  24. }
  25. }()
  26. return func() {
  27. if err := srv.Shutdown(context.Background()); err != nil {
  28. log.Panic("Server Shutdown: %s\n", err)
  29. }
  30. }
  31. }
  32. func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
  33. group.Use(CheckingKey(config.PluginInnerApiKey))
  34. group.Use(app.RedirectPluginInvoke())
  35. group.Use(app.InitClusterID())
  36. group.POST("/tool/invoke", controllers.InvokeTool)
  37. group.POST("/tool/validate_credentials", controllers.ValidateToolCredentials)
  38. group.POST("/llm/invoke", controllers.InvokeLLM)
  39. group.POST("/text_embedding/invoke", controllers.InvokeTextEmbedding)
  40. group.POST("/rerank/invoke", controllers.InvokeRerank)
  41. group.POST("/tts/invoke", controllers.InvokeTTS)
  42. group.POST("/speech2text/invoke", controllers.InvokeSpeech2Text)
  43. group.POST("/moderation/invoke", controllers.InvokeModeration)
  44. group.POST("/model/validate_provider_credentials", controllers.ValidateProviderCredentials)
  45. group.POST("/model/validate_model_credentials", controllers.ValidateModelCredentials)
  46. }
  47. func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
  48. if config.PluginRemoteInstallingEnabled {
  49. group.POST("/key", CheckingKey(config.PluginInnerApiKey), controllers.GetRemoteDebuggingKey)
  50. }
  51. }
  52. func (app *App) webhookGroup(group *gin.RouterGroup, config *app.Config) {
  53. if config.PluginWebhookEnabled {
  54. group.HEAD("/:hook_id/*path", app.Webhook())
  55. group.POST("/:hook_id/*path", app.Webhook())
  56. group.GET("/:hook_id/*path", app.Webhook())
  57. group.PUT("/:hook_id/*path", app.Webhook())
  58. group.DELETE("/:hook_id/*path", app.Webhook())
  59. group.OPTIONS("/:hook_id/*path", app.Webhook())
  60. }
  61. }