http_server.go 2.9 KB

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