config.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package app
  2. import (
  3. "fmt"
  4. "github.com/go-playground/validator/v10"
  5. )
  6. type Config struct {
  7. // server
  8. ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
  9. ServerKey string `envconfig:"SERVER_KEY" validate:"required"`
  10. // dify inner api
  11. DifyInnerApiURL string `envconfig:"DIFY_INNER_API_URL" validate:"required"`
  12. DifyInnerApiKey string `envconfig:"DIFY_INNER_API_KEY" validate:"required"`
  13. S3UseAwsManagedIam bool `envconfig:"S3_USE_AWS_MANAGED_IAM" default:"true"`
  14. S3Endpoint string `envconfig:"S3_ENDPOINT"`
  15. AWSAccessKey string `envconfig:"AWS_ACCESS_KEY"`
  16. AWSSecretKey string `envconfig:"AWS_SECRET_KEY"`
  17. AWSRegion string `envconfig:"AWS_REGION"`
  18. PluginStorageType string `envconfig:"PLUGIN_STORAGE_TYPE" validate:"required,oneof=local aws_s3"`
  19. PluginStorageOSSBucket string `envconfig:"PLUGIN_STORAGE_OSS_BUCKET"`
  20. PluginStorageLocalRoot string `envconfig:"PLUGIN_STORAGE_LOCAL_ROOT"`
  21. // plugin remote installing
  22. PluginRemoteInstallingHost string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
  23. PluginRemoteInstallingPort uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
  24. PluginRemoteInstallingEnabled *bool `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
  25. PluginRemoteInstallingMaxConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
  26. PluginRemoteInstallingMaxSingleTenantConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_SINGLE_TENANT_CONN"`
  27. PluginRemoteInstallServerEventLoopNums int `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
  28. // plugin endpoint
  29. PluginEndpointEnabled *bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
  30. // storage
  31. PluginWorkingPath string `envconfig:"PLUGIN_WORKING_PATH"` // where the plugin finally running
  32. PluginMediaCacheSize uint16 `envconfig:"PLUGIN_MEDIA_CACHE_SIZE"`
  33. PluginMediaCachePath string `envconfig:"PLUGIN_MEDIA_CACHE_PATH"`
  34. PluginInstalledPath string `envconfig:"PLUGIN_INSTALLED_PATH" validate:"required"` // where the plugin finally installed
  35. PluginPackageCachePath string `envconfig:"PLUGIN_PACKAGE_CACHE_PATH"` // where plugin packages stored
  36. // request timeout
  37. PluginMaxExecutionTimeout int `envconfig:"PLUGIN_MAX_EXECUTION_TIMEOUT" validate:"required"`
  38. // local launching max concurrent
  39. PluginLocalLaunchingConcurrent int `envconfig:"PLUGIN_LOCAL_LAUNCHING_CONCURRENT" validate:"required"`
  40. // platform like local or aws lambda
  41. Platform PlatformType `envconfig:"PLATFORM" validate:"required"`
  42. // routine pool
  43. RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
  44. // redis
  45. RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
  46. RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
  47. RedisPass string `envconfig:"REDIS_PASSWORD"`
  48. RedisUseSsl bool `envconfig:"REDIS_USE_SSL"`
  49. // database
  50. DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
  51. DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
  52. DBHost string `envconfig:"DB_HOST" validate:"required"`
  53. DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
  54. DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
  55. DBDefaultDatabase string `envconfig:"DB_DEFAULT_DATABASE" validate:"required"`
  56. DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
  57. // persistence storage
  58. PersistenceStoragePath string `envconfig:"PERSISTENCE_STORAGE_PATH"`
  59. PersistenceStorageMaxSize int64 `envconfig:"PERSISTENCE_STORAGE_MAX_SIZE"`
  60. // force verifying signature for all plugins, not allowing install plugin not signed
  61. ForceVerifyingSignature *bool `envconfig:"FORCE_VERIFYING_SIGNATURE"`
  62. // lifetime state management
  63. LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL" validate:"required"`
  64. LifetimeCollectionGCInterval int `envconfig:"LIFETIME_COLLECTION_GC_INTERVAL" validate:"required"`
  65. LifetimeStateGCInterval int `envconfig:"LIFETIME_STATE_GC_INTERVAL" validate:"required"`
  66. DifyInvocationConnectionIdleTimeout int `envconfig:"DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT" validate:"required"`
  67. DifyPluginServerlessConnectorURL *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_URL"`
  68. DifyPluginServerlessConnectorAPIKey *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY"`
  69. MaxPluginPackageSize int64 `envconfig:"MAX_PLUGIN_PACKAGE_SIZE" validate:"required"`
  70. MaxBundlePackageSize int64 `envconfig:"MAX_BUNDLE_PACKAGE_SIZE" validate:"required"`
  71. MaxServerlessTransactionTimeout int `envconfig:"MAX_SERVERLESS_TRANSACTION_TIMEOUT"`
  72. PythonInterpreterPath string `envconfig:"PYTHON_INTERPRETER_PATH"`
  73. PythonEnvInitTimeout int `envconfig:"PYTHON_ENV_INIT_TIMEOUT" validate:"required"`
  74. PipMirrorUrl string `envconfig:"PIP_MIRROR_URL"`
  75. PipPreferBinary *bool `envconfig:"PIP_PREFER_BINARY"`
  76. PipVerbose *bool `envconfig:"PIP_VERBOSE"`
  77. PipExtraArgs string `envconfig:"PIP_EXTRA_ARGS"`
  78. DisplayClusterLog bool `envconfig:"DISPLAY_CLUSTER_LOG"`
  79. PPROFEnabled bool `envconfig:"PPROF_ENABLED"`
  80. SentryEnabled bool `envconfig:"SENTRY_ENABLED"`
  81. SentryDSN string `envconfig:"SENTRY_DSN"`
  82. SentryAttachStacktrace bool `envconfig:"SENTRY_ATTACH_STACKTRACE"`
  83. SentryTracingEnabled bool `envconfig:"SENTRY_TRACING_ENABLED"`
  84. SentryTracesSampleRate float64 `envconfig:"SENTRY_TRACES_SAMPLE_RATE"`
  85. SentrySampleRate float64 `envconfig:"SENTRY_SAMPLE_RATE"`
  86. // proxy settings
  87. HttpProxy string `envconfig:"HTTP_PROXY"`
  88. HttpsProxy string `envconfig:"HTTPS_PROXY"`
  89. // log settings
  90. HealthApiLogEnabled *bool `envconfig:"HEALTH_API_LOG_ENABLED"`
  91. }
  92. func (c *Config) Validate() error {
  93. validator := validator.New()
  94. err := validator.Struct(c)
  95. if err != nil {
  96. return err
  97. }
  98. if c.PluginRemoteInstallingEnabled != nil && *c.PluginRemoteInstallingEnabled {
  99. if c.PluginRemoteInstallingHost == "" {
  100. return fmt.Errorf("plugin remote installing host is empty")
  101. }
  102. if c.PluginRemoteInstallingPort == 0 {
  103. return fmt.Errorf("plugin remote installing port is empty")
  104. }
  105. if c.PluginRemoteInstallingMaxConn == 0 {
  106. return fmt.Errorf("plugin remote installing max connection is empty")
  107. }
  108. if c.PluginRemoteInstallServerEventLoopNums == 0 {
  109. return fmt.Errorf("plugin remote install server event loop nums is empty")
  110. }
  111. }
  112. if c.Platform == PLATFORM_SERVERLESS {
  113. if c.DifyPluginServerlessConnectorURL == nil {
  114. return fmt.Errorf("dify plugin serverless connector url is empty")
  115. }
  116. if c.DifyPluginServerlessConnectorAPIKey == nil {
  117. return fmt.Errorf("dify plugin serverless connector api key is empty")
  118. }
  119. if c.MaxServerlessTransactionTimeout == 0 {
  120. return fmt.Errorf("max serverless transaction timeout is empty")
  121. }
  122. } else if c.Platform == PLATFORM_LOCAL {
  123. if c.PluginWorkingPath == "" {
  124. return fmt.Errorf("plugin working path is empty")
  125. }
  126. } else {
  127. return fmt.Errorf("invalid platform")
  128. }
  129. if c.PluginPackageCachePath == "" {
  130. return fmt.Errorf("plugin package cache path is empty")
  131. }
  132. if c.PluginStorageType == "aws_s3" {
  133. if c.PluginStorageOSSBucket == "" {
  134. return fmt.Errorf("plugin storage bucket is empty")
  135. }
  136. if c.AWSRegion == "" {
  137. return fmt.Errorf("aws region is empty")
  138. }
  139. }
  140. return nil
  141. }
  142. type PlatformType string
  143. const (
  144. PLATFORM_LOCAL PlatformType = "local"
  145. PLATFORM_SERVERLESS PlatformType = "serverless"
  146. )