config.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
  24. if err == nil {
  25. difySandboxGlobalConfigurations.App.Debug = debug
  26. }
  27. max_workers := os.Getenv("MAX_WORKERS")
  28. if max_workers != "" {
  29. difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)
  30. }
  31. max_requests := os.Getenv("MAX_REQUESTS")
  32. if max_requests != "" {
  33. difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)
  34. }
  35. port := os.Getenv("SANDBOX_PORT")
  36. if port != "" {
  37. difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
  38. }
  39. timeout := os.Getenv("WORKER_TIMEOUT")
  40. if timeout != "" {
  41. difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
  42. }
  43. api_key := os.Getenv("API_KEY")
  44. if api_key != "" {
  45. difySandboxGlobalConfigurations.App.Key = api_key
  46. }
  47. python_path := os.Getenv("PYTHON_PATH")
  48. if python_path != "" {
  49. difySandboxGlobalConfigurations.PythonPath = python_path
  50. }
  51. if difySandboxGlobalConfigurations.PythonPath == "" {
  52. difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"
  53. }
  54. nodejs_path := os.Getenv("NODEJS_PATH")
  55. if nodejs_path != "" {
  56. difySandboxGlobalConfigurations.NodejsPath = nodejs_path
  57. }
  58. if difySandboxGlobalConfigurations.NodejsPath == "" {
  59. difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"
  60. }
  61. return nil
  62. }
  63. // avoid global modification, use value copy instead
  64. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  65. return difySandboxGlobalConfigurations
  66. }