config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package app
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/go-playground/validator/v10"
  6. )
  7. type Config struct {
  8. ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
  9. PluginInnerApiKey string `envconfig:"PLUGIN_INNER_API_KEY" validate:"required"`
  10. PluginInnerApiURL string `envconfig:"PLUGIN_INNER_API_URL" validate:"required"`
  11. PluginRemoteInstallingHost string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
  12. PluginRemoteInstallingPort uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
  13. PluginRemoteInstallingEnabled bool `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
  14. PluginRemoteInstallingMaxConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
  15. PluginRemoteInstallServerEventLoopNums int `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
  16. PluginWebhookEnabled bool `envconfig:"PLUGIN_WEBHOOK_ENABLED"`
  17. PluginStoragePath string `envconfig:"STORAGE_PLUGIN_PATH" validate:"required"`
  18. PluginWorkingPath string `envconfig:"PLUGIN_WORKING_PATH"`
  19. ProcessCachingPath string `envconfig:"PROCESS_CACHING_PATH"`
  20. Platform PlatformType `envconfig:"PLATFORM" validate:"required"`
  21. RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
  22. RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
  23. RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
  24. RedisPass string `envconfig:"REDIS_PASS" validate:"required"`
  25. DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
  26. DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
  27. DBHost string `envconfig:"DB_HOST" validate:"required"`
  28. DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
  29. DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
  30. DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
  31. LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL" validate:"required"`
  32. LifetimeCollectionGCInterval int `envconfig:"LIFETIME_COLLECTION_GC_INTERVAL" validate:"required"`
  33. LifetimeStateGCInterval int `envconfig:"LIFETIME_STATE_GC_INTERVAL" validate:"required"`
  34. DifyInvocationConnectionIdleTimeout int `envconfig:"DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT" validate:"required"`
  35. DifyPluginServerlessConnectorURL *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_URL"`
  36. DifyPluginServerlessConnectorAPIKey *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY"`
  37. MaxPluginPackageSize int64 `envconfig:"MAX_PLUGIN_PACKAGE_SIZE" validate:"required"`
  38. MaxAWSLambdaTransactionTimeout time.Duration `envconfig:"MAX_AWS_LAMBDA_TRANSACTION_TIMEOUT"`
  39. }
  40. func (c *Config) Validate() error {
  41. validator := validator.New()
  42. err := validator.Struct(c)
  43. if err != nil {
  44. return err
  45. }
  46. if c.PluginRemoteInstallingEnabled {
  47. if c.PluginRemoteInstallingHost == "" {
  48. return fmt.Errorf("plugin remote installing host is empty")
  49. }
  50. if c.PluginRemoteInstallingPort == 0 {
  51. return fmt.Errorf("plugin remote installing port is empty")
  52. }
  53. if c.PluginRemoteInstallingMaxConn == 0 {
  54. return fmt.Errorf("plugin remote installing max connection is empty")
  55. }
  56. if c.PluginRemoteInstallServerEventLoopNums == 0 {
  57. return fmt.Errorf("plugin remote install server event loop nums is empty")
  58. }
  59. }
  60. if c.Platform == PLATFORM_AWS_LAMBDA {
  61. if c.DifyPluginServerlessConnectorURL == nil {
  62. return fmt.Errorf("dify plugin serverless connector url is empty")
  63. }
  64. if c.DifyPluginServerlessConnectorAPIKey == nil {
  65. return fmt.Errorf("dify plugin serverless connector api key is empty")
  66. }
  67. if c.MaxAWSLambdaTransactionTimeout == 0 {
  68. return fmt.Errorf("max aws lambda transaction timeout is empty")
  69. }
  70. } else if c.Platform == PLATFORM_LOCAL {
  71. if c.PluginWorkingPath == "" {
  72. return fmt.Errorf("plugin working path is empty")
  73. }
  74. if c.ProcessCachingPath == "" {
  75. return fmt.Errorf("process caching path is empty")
  76. }
  77. } else {
  78. return fmt.Errorf("invalid platform")
  79. }
  80. return nil
  81. }
  82. type PlatformType string
  83. const (
  84. PLATFORM_LOCAL PlatformType = "local"
  85. PLATFORM_AWS_LAMBDA PlatformType = "aws_lambda"
  86. )