config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package static
  2. import (
  3. "os"
  4. "strconv"
  5. "github.com/langgenius/dify-sandbox/internal/types"
  6. "gopkg.in/yaml.v3"
  7. )
  8. var difySandboxGlobalConfigurations types.DifySandboxGlobalConfigurations
  9. func InitConfig(path string) error {
  10. difySandboxGlobalConfigurations = types.DifySandboxGlobalConfigurations{}
  11. // read config file
  12. configFile, err := os.Open(path)
  13. if err != nil {
  14. return err
  15. }
  16. defer configFile.Close()
  17. // parse config file
  18. decoder := yaml.NewDecoder(configFile)
  19. err = decoder.Decode(&difySandboxGlobalConfigurations)
  20. if err != nil {
  21. return err
  22. }
  23. max_workers := os.Getenv("MAX_WORKERS")
  24. if max_workers != "" {
  25. difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)
  26. }
  27. port := os.Getenv("SANDBOX_PORT")
  28. if port != "" {
  29. difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
  30. }
  31. timeout := os.Getenv("WORKER_TIMEOUT")
  32. if timeout != "" {
  33. difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
  34. }
  35. return nil
  36. }
  37. // avoid global modification, use value copy instead
  38. func GetCoshubGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  39. return difySandboxGlobalConfigurations
  40. }