config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. PluginInnerApiKey string `envconfig:"PLUGIN_INNER_API_KEY" validate:"required"`
  9. PluginInnerApiURL string `envconfig:"PLUGIN_INNER_API_URL" validate:"required"`
  10. PluginRemoteInstallingHost string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
  11. PluginRemoteInstallingPort uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
  12. PluginRemoteInstallingEnabled bool `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
  13. PluginRemoteInstallingMaxConn int `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
  14. PluginRemoteInstallServerEventLoopNums int `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
  15. PluginWebhookEnabled bool `envconfig:"PLUGIN_WEBHOOK_ENABLED"`
  16. PluginStoragePath string `envconfig:"STORAGE_PLUGIN_PATH" validate:"required"`
  17. PluginWorkingPath string `envconfig:"PLUGIN_WORKING_PATH"`
  18. ProcessCachingPath string `envconfig:"PROCESS_CACHING_PATH"`
  19. Platform PlatformType `envconfig:"PLATFORM" validate:"required"`
  20. RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
  21. RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
  22. RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
  23. RedisPass string `envconfig:"REDIS_PASS" validate:"required"`
  24. DBUsername string `envconfig:"DB_USERNAME" validate:"required"`
  25. DBPassword string `envconfig:"DB_PASSWORD" validate:"required"`
  26. DBHost string `envconfig:"DB_HOST" validate:"required"`
  27. DBPort uint16 `envconfig:"DB_PORT" validate:"required"`
  28. DBDatabase string `envconfig:"DB_DATABASE" validate:"required"`
  29. DBSslMode string `envconfig:"DB_SSL_MODE" validate:"required,oneof=disable require"`
  30. LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL" validate:"required"`
  31. LifetimeCollectionGCInterval int `envconfig:"LIFETIME_COLLECTION_GC_INTERVAL" validate:"required"`
  32. LifetimeStateGCInterval int `envconfig:"LIFETIME_STATE_GC_INTERVAL" validate:"required"`
  33. DifyInvocationConnectionIdleTimeout int `envconfig:"DIFY_INVOCATION_CONNECTION_IDLE_TIMEOUT" validate:"required"`
  34. DifyPluginServerlessConnectorURL *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_URL"`
  35. DifyPluginServerlessConnectorAPIKey *string `envconfig:"DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY"`
  36. MaxPluginPackageSize int64 `envconfig:"MAX_PLUGIN_PACKAGE_SIZE" validate:"required"`
  37. }
  38. func (c *Config) Validate() error {
  39. validator := validator.New()
  40. err := validator.Struct(c)
  41. if err != nil {
  42. return err
  43. }
  44. if c.PluginRemoteInstallingEnabled {
  45. if c.PluginRemoteInstallingHost == "" {
  46. return fmt.Errorf("plugin remote installing host is empty")
  47. }
  48. if c.PluginRemoteInstallingPort == 0 {
  49. return fmt.Errorf("plugin remote installing port is empty")
  50. }
  51. if c.PluginRemoteInstallingMaxConn == 0 {
  52. return fmt.Errorf("plugin remote installing max connection is empty")
  53. }
  54. if c.PluginRemoteInstallServerEventLoopNums == 0 {
  55. return fmt.Errorf("plugin remote install server event loop nums is empty")
  56. }
  57. }
  58. if c.Platform == PLATFORM_AWS_LAMBDA {
  59. if c.DifyPluginServerlessConnectorURL == nil {
  60. return fmt.Errorf("dify plugin serverless connector url is empty")
  61. }
  62. if c.DifyPluginServerlessConnectorAPIKey == nil {
  63. return fmt.Errorf("dify plugin serverless connector api key is empty")
  64. }
  65. } else if c.Platform == PLATFORM_LOCAL {
  66. if c.PluginWorkingPath == "" {
  67. return fmt.Errorf("plugin working path is empty")
  68. }
  69. if c.ProcessCachingPath == "" {
  70. return fmt.Errorf("process caching path is empty")
  71. }
  72. } else {
  73. return fmt.Errorf("invalid platform")
  74. }
  75. return nil
  76. }
  77. type PlatformType string
  78. const (
  79. PLATFORM_LOCAL PlatformType = "local"
  80. PLATFORM_AWS_LAMBDA PlatformType = "aws_lambda"
  81. )