config.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // plugin remote installing
  14. PluginRemoteInstallingHost string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
  15. PluginRemoteInstallingPort uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
  16. PluginRemoteInstallingEnabled bool `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
  17. PluginRemoteInstallingMaxConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
  18. PluginRemoteInstallServerEventLoopNums int `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
  19. PluginEndpointEnabled bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
  20. PluginStoragePath string `envconfig:"STORAGE_PLUGIN_PATH" validate:"required"`
  21. PluginPackageCachePath string `envconfig:"PLUGIN_PACKAGE_CACHE_PATH"`
  22. PluginWorkingPath string `envconfig:"PLUGIN_WORKING_PATH"`
  23. PluginMediaCacheSize uint16 `envconfig:"PLUGIN_MEDIA_CACHE_SIZE"`
  24. PluginMediaCachePath string `envconfig:"PLUGIN_MEDIA_CACHE_PATH"`
  25. ProcessCachingPath string `envconfig:"PROCESS_CACHING_PATH"`
  26. PluginMaxExecutionTimeout int `envconfig:"PLUGIN_MAX_EXECUTION_TIMEOUT" validate:"required"`
  27. // platform like local or aws lambda
  28. Platform PlatformType `envconfig:"PLATFORM" validate:"required"`
  29. // routine pool
  30. RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
  31. // redis
  32. RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
  33. RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
  34. RedisPass string `envconfig:"REDIS_PASS" validate:"required"`
  35. // database
  36. DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
  37. DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
  38. DBHost string `envconfig:"DB_HOST" validate:"required"`
  39. DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
  40. DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
  41. DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
  42. // persistence storage
  43. PersistenceStorageType string `envconfig:"PERSISTENCE_STORAGE_TYPE" validate:"required,oneof=local s3"`
  44. PersistenceStorageLocalPath string `envconfig:"PERSISTENCE_STORAGE_LOCAL_PATH"`
  45. PersistenceStorageS3Region string `envconfig:"PERSISTENCE_STORAGE_S3_REGION"`
  46. PersistenceStorageS3AccessKey string `envconfig:"PERSISTENCE_STORAGE_S3_ACCESS_KEY"`
  47. PersistenceStorageS3SecretKey string `envconfig:"PERSISTENCE_STORAGE_S3_SECRET_KEY"`
  48. PersistenceStorageS3Bucket string `envconfig:"PERSISTENCE_STORAGE_S3_BUCKET"`
  49. // force verifying signature for all plugins, not allowing install plugin not signed
  50. ForceVerifyingSignature bool `envconfig:"FORCE_VERIFYING_SIGNATURE"`
  51. // lifetime state management
  52. LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL" validate:"required"`
  53. LifetimeCollectionGCInterval int `envconfig:"LIFETIME_COLLECTION_GC_INTERVAL" validate:"required"`
  54. LifetimeStateGCInterval int `envconfig:"LIFETIME_STATE_GC_INTERVAL" validate:"required"`
  55. DifyInvocationConnectionIdleTimeout int `envconfig:"DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT" validate:"required"`
  56. DifyPluginServerlessConnectorURL *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_URL"`
  57. DifyPluginServerlessConnectorAPIKey *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY"`
  58. MaxPluginPackageSize int64 `envconfig:"MAX_PLUGIN_PACKAGE_SIZE" validate:"required"`
  59. MaxAWSLambdaTransactionTimeout int `envconfig:"MAX_AWS_LAMBDA_TRANSACTION_TIMEOUT"`
  60. }
  61. func (c *Config) Validate() error {
  62. validator := validator.New()
  63. err := validator.Struct(c)
  64. if err != nil {
  65. return err
  66. }
  67. if c.PluginRemoteInstallingEnabled {
  68. if c.PluginRemoteInstallingHost == "" {
  69. return fmt.Errorf("plugin remote installing host is empty")
  70. }
  71. if c.PluginRemoteInstallingPort == 0 {
  72. return fmt.Errorf("plugin remote installing port is empty")
  73. }
  74. if c.PluginRemoteInstallingMaxConn == 0 {
  75. return fmt.Errorf("plugin remote installing max connection is empty")
  76. }
  77. if c.PluginRemoteInstallServerEventLoopNums == 0 {
  78. return fmt.Errorf("plugin remote install server event loop nums is empty")
  79. }
  80. }
  81. if c.Platform == PLATFORM_AWS_LAMBDA {
  82. if c.DifyPluginServerlessConnectorURL == nil {
  83. return fmt.Errorf("dify plugin serverless connector url is empty")
  84. }
  85. if c.DifyPluginServerlessConnectorAPIKey == nil {
  86. return fmt.Errorf("dify plugin serverless connector api key is empty")
  87. }
  88. if c.MaxAWSLambdaTransactionTimeout == 0 {
  89. return fmt.Errorf("max aws lambda transaction timeout is empty")
  90. }
  91. } else if c.Platform == PLATFORM_LOCAL {
  92. if c.PluginWorkingPath == "" {
  93. return fmt.Errorf("plugin working path is empty")
  94. }
  95. if c.ProcessCachingPath == "" {
  96. return fmt.Errorf("process caching path is empty")
  97. }
  98. } else {
  99. return fmt.Errorf("invalid platform")
  100. }
  101. if c.PersistenceStorageType == "s3" {
  102. if c.PersistenceStorageS3Region == "" ||
  103. c.PersistenceStorageS3AccessKey == "" ||
  104. c.PersistenceStorageS3SecretKey == "" ||
  105. c.PersistenceStorageS3Bucket == "" {
  106. return fmt.Errorf("s3 region, access key, secret key or bucket is empty")
  107. }
  108. }
  109. if c.PluginPackageCachePath == "" {
  110. return fmt.Errorf("plugin package cache path is empty")
  111. }
  112. return nil
  113. }
  114. type PlatformType string
  115. const (
  116. PLATFORM_LOCAL PlatformType = "local"
  117. PLATFORM_AWS_LAMBDA PlatformType = "aws_lambda"
  118. )