config.go 7.8 KB

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