config.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. api_key := os.Getenv("API_KEY")
  40. if api_key != "" {
  41. difySandboxGlobalConfigurations.App.Key = api_key
  42. }
  43. python_path := os.Getenv("PYTHON_PATH")
  44. if python_path != "" {
  45. difySandboxGlobalConfigurations.PythonPath = python_path
  46. }
  47. if difySandboxGlobalConfigurations.PythonPath == "" {
  48. difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"
  49. }
  50. return nil
  51. }
  52. // avoid global modification, use value copy instead
  53. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  54. return difySandboxGlobalConfigurations
  55. }