config.go 6.7 KB

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