1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package static
- import (
- "os"
- "strconv"
- "github.com/langgenius/dify-sandbox/internal/types"
- "gopkg.in/yaml.v3"
- )
- var difySandboxGlobalConfigurations types.DifySandboxGlobalConfigurations
- func InitConfig(path string) error {
- difySandboxGlobalConfigurations = types.DifySandboxGlobalConfigurations{}
- // read config file
- configFile, err := os.Open(path)
- if err != nil {
- return err
- }
- defer configFile.Close()
- // parse config file
- decoder := yaml.NewDecoder(configFile)
- err = decoder.Decode(&difySandboxGlobalConfigurations)
- if err != nil {
- return err
- }
- debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
- if err == nil {
- difySandboxGlobalConfigurations.App.Debug = debug
- }
- max_workers := os.Getenv("MAX_WORKERS")
- if max_workers != "" {
- difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)
- }
- max_requests := os.Getenv("MAX_REQUESTS")
- if max_requests != "" {
- difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)
- }
- port := os.Getenv("SANDBOX_PORT")
- if port != "" {
- difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
- }
- timeout := os.Getenv("WORKER_TIMEOUT")
- if timeout != "" {
- difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
- }
- api_key := os.Getenv("API_KEY")
- if api_key != "" {
- difySandboxGlobalConfigurations.App.Key = api_key
- }
- python_path := os.Getenv("PYTHON_PATH")
- if python_path != "" {
- difySandboxGlobalConfigurations.PythonPath = python_path
- }
- if difySandboxGlobalConfigurations.PythonPath == "" {
- difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"
- }
- nodejs_path := os.Getenv("NODEJS_PATH")
- if nodejs_path != "" {
- difySandboxGlobalConfigurations.NodejsPath = nodejs_path
- }
- if difySandboxGlobalConfigurations.NodejsPath == "" {
- difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"
- }
- return nil
- }
- // avoid global modification, use value copy instead
- func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
- return difySandboxGlobalConfigurations
- }
|