http_server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/core/plugin_daemon/backwards_invocation/transaction"
  8. "github.com/langgenius/dify-plugin-daemon/internal/server/controllers"
  9. "github.com/langgenius/dify-plugin-daemon/internal/service"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  12. )
  13. func (app *App) server(config *app.Config) func() {
  14. engine := gin.Default()
  15. engine.GET("/health/check", controllers.HealthCheck)
  16. app.pluginInvokeGroup(engine.Group("/plugin"), config)
  17. app.remoteDebuggingGroup(engine.Group("/plugin/debugging"), config)
  18. app.webhookGroup(engine.Group("/webhook"), config)
  19. app.awsLambdaTransactionGroup(engine.Group("/aws-lambda"), config)
  20. srv := &http.Server{
  21. Addr: fmt.Sprintf(":%d", config.ServerPort),
  22. Handler: engine,
  23. }
  24. go func() {
  25. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  26. log.Panic("listen: %s\n", err)
  27. }
  28. }()
  29. return func() {
  30. if err := srv.Shutdown(context.Background()); err != nil {
  31. log.Panic("Server Shutdown: %s\n", err)
  32. }
  33. }
  34. }
  35. func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
  36. group.Use(CheckingKey(config.PluginInnerApiKey))
  37. group.Use(app.RedirectPluginInvoke())
  38. group.Use(app.InitClusterID())
  39. group.POST("/tool/invoke", controllers.InvokeTool)
  40. group.POST("/tool/validate_credentials", controllers.ValidateToolCredentials)
  41. group.POST("/llm/invoke", controllers.InvokeLLM)
  42. group.POST("/text_embedding/invoke", controllers.InvokeTextEmbedding)
  43. group.POST("/rerank/invoke", controllers.InvokeRerank)
  44. group.POST("/tts/invoke", controllers.InvokeTTS)
  45. group.POST("/speech2text/invoke", controllers.InvokeSpeech2Text)
  46. group.POST("/moderation/invoke", controllers.InvokeModeration)
  47. group.POST("/model/validate_provider_credentials", controllers.ValidateProviderCredentials)
  48. group.POST("/model/validate_model_credentials", controllers.ValidateModelCredentials)
  49. }
  50. func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
  51. if config.PluginRemoteInstallingEnabled {
  52. group.POST("/key", CheckingKey(config.PluginInnerApiKey), controllers.GetRemoteDebuggingKey)
  53. }
  54. }
  55. func (app *App) webhookGroup(group *gin.RouterGroup, config *app.Config) {
  56. if config.PluginWebhookEnabled {
  57. group.HEAD("/:hook_id/*path", app.Webhook())
  58. group.POST("/:hook_id/*path", app.Webhook())
  59. group.GET("/:hook_id/*path", app.Webhook())
  60. group.PUT("/:hook_id/*path", app.Webhook())
  61. group.DELETE("/:hook_id/*path", app.Webhook())
  62. group.OPTIONS("/:hook_id/*path", app.Webhook())
  63. }
  64. }
  65. func (appRef *App) awsLambdaTransactionGroup(group *gin.RouterGroup, config *app.Config) {
  66. if config.Platform == app.PLATFORM_AWS_LAMBDA {
  67. appRef.aws_transaction_handler = transaction.NewAWSTransactionHandler(config.MaxAWSLambdaTransactionTimeout)
  68. group.POST(
  69. "/transaction",
  70. appRef.RedirectAWSLambdaTransaction,
  71. service.HandleAWSPluginTransaction(appRef.aws_transaction_handler),
  72. )
  73. }
  74. }