http_server.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/backwards_invocation/transaction"
  9. "github.com/langgenius/dify-plugin-daemon/internal/server/controllers"
  10. "github.com/langgenius/dify-plugin-daemon/internal/service"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  13. sentrygin "github.com/getsentry/sentry-go/gin"
  14. )
  15. // server starts a http server and returns a function to stop it
  16. func (app *App) server(config *app.Config) func() {
  17. engine := gin.New()
  18. if *config.HealthApiLogEnabled {
  19. engine.Use(gin.Logger())
  20. } else {
  21. engine.Use(gin.LoggerWithConfig(gin.LoggerConfig{
  22. SkipPaths: []string{"/health/check"},
  23. }))
  24. }
  25. engine.Use(gin.Recovery())
  26. engine.GET("/health/check", controllers.HealthCheck(config))
  27. endpointGroup := engine.Group("/e")
  28. awsLambdaTransactionGroup := engine.Group("/backwards-invocation")
  29. pluginGroup := engine.Group("/plugin/:tenant_id")
  30. pprofGroup := engine.Group("/debug/pprof")
  31. if config.SentryEnabled {
  32. // setup sentry for all groups
  33. sentryGroup := []*gin.RouterGroup{
  34. endpointGroup,
  35. awsLambdaTransactionGroup,
  36. pluginGroup,
  37. }
  38. for _, group := range sentryGroup {
  39. group.Use(sentrygin.New(sentrygin.Options{
  40. Repanic: true,
  41. }))
  42. }
  43. }
  44. app.endpointGroup(endpointGroup, config)
  45. app.awsLambdaTransactionGroup(awsLambdaTransactionGroup, config)
  46. app.pluginGroup(pluginGroup, config)
  47. app.pprofGroup(pprofGroup, config)
  48. srv := &http.Server{
  49. Addr: fmt.Sprintf(":%d", config.ServerPort),
  50. Handler: engine,
  51. }
  52. go func() {
  53. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  54. log.Panic("listen: %s\n", err)
  55. }
  56. }()
  57. return func() {
  58. if err := srv.Shutdown(context.Background()); err != nil {
  59. log.Panic("Server Shutdown: %s\n", err)
  60. }
  61. }
  62. }
  63. func (app *App) pluginGroup(group *gin.RouterGroup, config *app.Config) {
  64. group.Use(CheckingKey(config.ServerKey))
  65. app.remoteDebuggingGroup(group.Group("/debugging"), config)
  66. app.pluginDispatchGroup(group.Group("/dispatch"), config)
  67. app.pluginManagementGroup(group.Group("/management"), config)
  68. app.endpointManagementGroup(group.Group("/endpoint"))
  69. app.pluginAssetGroup(group.Group("/asset"))
  70. }
  71. func (app *App) pluginDispatchGroup(group *gin.RouterGroup, config *app.Config) {
  72. group.Use(app.FetchPluginInstallation())
  73. group.Use(app.RedirectPluginInvoke())
  74. group.Use(app.InitClusterID())
  75. group.POST("/tool/invoke", controllers.InvokeTool(config))
  76. group.POST("/tool/validate_credentials", controllers.ValidateToolCredentials(config))
  77. group.POST("/tool/get_runtime_parameters", controllers.GetToolRuntimeParameters(config))
  78. group.POST("/agent_strategy/invoke", controllers.InvokeAgentStrategy(config))
  79. group.POST("/llm/invoke", controllers.InvokeLLM(config))
  80. group.POST("/llm/num_tokens", controllers.GetLLMNumTokens(config))
  81. group.POST("/text_embedding/invoke", controllers.InvokeTextEmbedding(config))
  82. group.POST("/text_embedding/num_tokens", controllers.GetTextEmbeddingNumTokens(config))
  83. group.POST("/rerank/invoke", controllers.InvokeRerank(config))
  84. group.POST("/tts/invoke", controllers.InvokeTTS(config))
  85. group.POST("/tts/model/voices", controllers.GetTTSModelVoices(config))
  86. group.POST("/speech2text/invoke", controllers.InvokeSpeech2Text(config))
  87. group.POST("/moderation/invoke", controllers.InvokeModeration(config))
  88. group.POST("/model/validate_provider_credentials", controllers.ValidateProviderCredentials(config))
  89. group.POST("/model/validate_model_credentials", controllers.ValidateModelCredentials(config))
  90. group.POST("/model/schema", controllers.GetAIModelSchema(config))
  91. }
  92. func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
  93. if config.PluginRemoteInstallingEnabled != nil && *config.PluginRemoteInstallingEnabled {
  94. group.POST("/key", CheckingKey(config.ServerKey), controllers.GetRemoteDebuggingKey)
  95. }
  96. }
  97. func (app *App) endpointGroup(group *gin.RouterGroup, config *app.Config) {
  98. if config.PluginEndpointEnabled != nil && *config.PluginEndpointEnabled {
  99. group.HEAD("/:hook_id/*path", app.Endpoint(config))
  100. group.POST("/:hook_id/*path", app.Endpoint(config))
  101. group.GET("/:hook_id/*path", app.Endpoint(config))
  102. group.PUT("/:hook_id/*path", app.Endpoint(config))
  103. group.DELETE("/:hook_id/*path", app.Endpoint(config))
  104. group.OPTIONS("/:hook_id/*path", app.Endpoint(config))
  105. }
  106. }
  107. func (appRef *App) awsLambdaTransactionGroup(group *gin.RouterGroup, config *app.Config) {
  108. if config.Platform == app.PLATFORM_SERVERLESS {
  109. appRef.awsTransactionHandler = transaction.NewAWSTransactionHandler(
  110. time.Duration(config.MaxServerlessTransactionTimeout) * time.Second,
  111. )
  112. group.POST(
  113. "/transaction",
  114. service.HandleAWSPluginTransaction(appRef.awsTransactionHandler),
  115. )
  116. }
  117. }
  118. func (app *App) endpointManagementGroup(group *gin.RouterGroup) {
  119. group.POST("/setup", controllers.SetupEndpoint)
  120. group.POST("/remove", controllers.RemoveEndpoint)
  121. group.POST("/update", controllers.UpdateEndpoint)
  122. group.GET("/list", controllers.ListEndpoints)
  123. group.GET("/list/plugin", controllers.ListPluginEndpoints)
  124. group.POST("/enable", controllers.EnableEndpoint)
  125. group.POST("/disable", controllers.DisableEndpoint)
  126. }
  127. func (app *App) pluginManagementGroup(group *gin.RouterGroup, config *app.Config) {
  128. group.POST("/install/upload/package", controllers.UploadPlugin(config))
  129. group.POST("/install/upload/bundle", controllers.UploadBundle(config))
  130. group.POST("/install/identifiers", controllers.InstallPluginFromIdentifiers(config))
  131. group.POST("/install/upgrade", controllers.UpgradePlugin(config))
  132. group.GET("/install/tasks/:id", controllers.FetchPluginInstallationTask)
  133. group.POST("/install/tasks/delete_all", controllers.DeleteAllPluginInstallationTasks)
  134. group.POST("/install/tasks/:id/delete", controllers.DeletePluginInstallationTask)
  135. group.POST("/install/tasks/:id/delete/*identifier", controllers.DeletePluginInstallationItemFromTask)
  136. group.GET("/install/tasks", controllers.FetchPluginInstallationTasks)
  137. group.GET("/fetch/manifest", controllers.FetchPluginManifest)
  138. group.GET("/fetch/identifier", controllers.FetchPluginFromIdentifier)
  139. group.POST("/uninstall", controllers.UninstallPlugin)
  140. group.GET("/list", controllers.ListPlugins)
  141. group.POST("/installation/fetch/batch", controllers.BatchFetchPluginInstallationByIDs)
  142. group.POST("/installation/missing", controllers.FetchMissingPluginInstallations)
  143. group.GET("/models", controllers.ListModels)
  144. group.GET("/tools", controllers.ListTools)
  145. group.GET("/tool", controllers.GetTool)
  146. group.POST("/tools/check_existence", controllers.CheckToolExistence)
  147. group.GET("/agent_strategies", controllers.ListAgentStrategies)
  148. group.GET("/agent_strategy", controllers.GetAgentStrategy)
  149. }
  150. func (app *App) pluginAssetGroup(group *gin.RouterGroup) {
  151. group.GET("/:id", controllers.GetAsset)
  152. }
  153. func (app *App) pprofGroup(group *gin.RouterGroup, config *app.Config) {
  154. if config.PPROFEnabled {
  155. group.Use(CheckingKey(config.ServerKey))
  156. group.GET("/", controllers.PprofIndex)
  157. group.GET("/cmdline", controllers.PprofCmdline)
  158. group.GET("/profile", controllers.PprofProfile)
  159. group.GET("/symbol", controllers.PprofSymbol)
  160. group.GET("/trace", controllers.PprofTrace)
  161. group.GET("/goroutine", controllers.PprofGoroutine)
  162. group.GET("/heap", controllers.PprofHeap)
  163. group.GET("/allocs", controllers.PprofAllocs)
  164. group.GET("/block", controllers.PprofBlock)
  165. group.GET("/mutex", controllers.PprofMutex)
  166. group.GET("/threadcreate", controllers.PprofThreadcreate)
  167. }
  168. }