http_server.go 7.4 KB

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