config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package app
  2. import (
  3. "fmt"
  4. "github.com/go-playground/validator/v10"
  5. )
  6. type Config struct {
  7. ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
  8. ServerKey string `envconfig:"SERVER_KEY" validate:"required"`
  9. PluginServerApiKey string `envconfig:"PLUGIN_SERVER_API_KEY" validate:"required"`
  10. DifyInnerApiURL string `envconfig:"DIFY_INNER_API_URL" validate:"required"`
  11. DifyInnerApiKey string `envconfig:"DIFY_INNER_API_KEY" validate:"required"`
  12. PluginRemoteInstallingHost string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
  13. PluginRemoteInstallingPort uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
  14. PluginRemoteInstallingEnabled bool `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
  15. PluginRemoteInstallingMaxConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
  16. PluginRemoteInstallServerEventLoopNums int `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
  17. PluginEndpointEnabled bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
  18. PluginStoragePath string `envconfig:"STORAGE_PLUGIN_PATH" validate:"required"`
  19. PluginWorkingPath string `envconfig:"PLUGIN_WORKING_PATH"`
  20. PluginMediaCacheSize uint16 `envconfig:"PLUGIN_MEDIA_CACHE_SIZE"`
  21. PluginMediaCachePath string `envconfig:"PLUGIN_MEDIA_CACHE_PATH"`
  22. ProcessCachingPath string `envconfig:"PROCESS_CACHING_PATH"`
  23. PluginMaxExecutionTimeout int `envconfig:"PLUGIN_MAX_EXECUTION_TIMEOUT" validate:"required"`
  24. Platform PlatformType `envconfig:"PLATFORM" validate:"required"`
  25. RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
  26. RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
  27. RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
  28. RedisPass string `envconfig:"REDIS_PASS" validate:"required"`
  29. DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
  30. DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
  31. DBHost string `envconfig:"DB_HOST" validate:"required"`
  32. DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
  33. DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
  34. DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
  35. PersistenceStorageType string `envconfig:"PERSISTENCE_STORAGE_TYPE" validate:"required,oneof=local s3"`
  36. PersistenceStorageLocalPath string `envconfig:"PERSISTENCE_STORAGE_LOCAL_PATH"`
  37. PersistenceStorageS3Region string `envconfig:"PERSISTENCE_STORAGE_S3_REGION"`
  38. PersistenceStorageS3AccessKey string `envconfig:"PERSISTENCE_STORAGE_S3_ACCESS_KEY"`
  39. PersistenceStorageS3SecretKey string `envconfig:"PERSISTENCE_STORAGE_S3_SECRET_KEY"`
  40. PersistenceStorageS3Bucket string `envconfig:"PERSISTENCE_STORAGE_S3_BUCKET"`
  41. LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL" validate:"required"`
  42. LifetimeCollectionGCInterval int `envconfig:"LIFETIME_COLLECTION_GC_INTERVAL" validate:"required"`
  43. LifetimeStateGCInterval int `envconfig:"LIFETIME_STATE_GC_INTERVAL" validate:"required"`
  44. DifyInvocationConnectionIdleTimeout int `envconfig:"DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT" validate:"required"`
  45. DifyPluginServerlessConnectorURL *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_URL"`
  46. DifyPluginServerlessConnectorAPIKey *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY"`
  47. MaxPluginPackageSize int64 `envconfig:"MAX_PLUGIN_PACKAGE_SIZE" validate:"required"`
  48. MaxAWSLambdaTransactionTimeout int `envconfig:"MAX_AWS_LAMBDA_TRANSACTION_TIMEOUT"`
  49. }
  50. func (c *Config) Validate() error {
  51. validator := validator.New()
  52. err := validator.Struct(c)
  53. if err != nil {
  54. return err
  55. }
  56. if c.PluginRemoteInstallingEnabled {
  57. if c.PluginRemoteInstallingHost == "" {
  58. return fmt.Errorf("plugin remote installing host is empty")
  59. }
  60. if c.PluginRemoteInstallingPort == 0 {
  61. return fmt.Errorf("plugin remote installing port is empty")
  62. }
  63. if c.PluginRemoteInstallingMaxConn == 0 {
  64. return fmt.Errorf("plugin remote installing max connection is empty")
  65. }
  66. if c.PluginRemoteInstallServerEventLoopNums == 0 {
  67. return fmt.Errorf("plugin remote install server event loop nums is empty")
  68. }
  69. }
  70. if c.Platform == PLATFORM_AWS_LAMBDA {
  71. if c.DifyPluginServerlessConnectorURL == nil {
  72. return fmt.Errorf("dify plugin serverless connector url is empty")
  73. }
  74. if c.DifyPluginServerlessConnectorAPIKey == nil {
  75. return fmt.Errorf("dify plugin serverless connector api key is empty")
  76. }
  77. if c.MaxAWSLambdaTransactionTimeout == 0 {
  78. return fmt.Errorf("max aws lambda transaction timeout is empty")
  79. }
  80. } else if c.Platform == PLATFORM_LOCAL {
  81. if c.PluginWorkingPath == "" {
  82. return fmt.Errorf("plugin working path is empty")
  83. }
  84. if c.ProcessCachingPath == "" {
  85. return fmt.Errorf("process caching path is empty")
  86. }
  87. } else {
  88. return fmt.Errorf("invalid platform")
  89. }
  90. if c.PersistenceStorageType == "s3" {
  91. if c.PersistenceStorageS3Region == "" ||
  92. c.PersistenceStorageS3AccessKey == "" ||
  93. c.PersistenceStorageS3SecretKey == "" ||
  94. c.PersistenceStorageS3Bucket == "" {
  95. return fmt.Errorf("s3 region, access key, secret key, bucket is empty")
  96. }
  97. }
  98. return nil
  99. }
  100. type PlatformType string
  101. const (
  102. PLATFORM_LOCAL PlatformType = "local"
  103. PLATFORM_AWS_LAMBDA PlatformType = "aws_lambda"
  104. )