config.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. max_requests := os.Getenv("MAX_REQUESTS")
  28. if max_requests != "" {
  29. difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)
  30. }
  31. port := os.Getenv("SANDBOX_PORT")
  32. if port != "" {
  33. difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
  34. }
  35. timeout := os.Getenv("WORKER_TIMEOUT")
  36. if timeout != "" {
  37. difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
  38. }
  39. return nil
  40. }
  41. // avoid global modification, use value copy instead
  42. func GetCoshubGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  43. return difySandboxGlobalConfigurations
  44. }