From e674f8266a7360e553fcd2c1177b3158d0cd5643 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 10 Sep 2024 10:53:09 +0800 Subject: [PATCH] Proxy: Remove dependency of godotenv. #4158 --- proxy/env.go | 43 +++++++++++++++++++++++++++++++++++-------- proxy/go.mod | 5 +---- proxy/go.sum | 2 -- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/proxy/env.go b/proxy/env.go index 0c201bb1d6..26b014609c 100644 --- a/proxy/env.go +++ b/proxy/env.go @@ -5,10 +5,10 @@ package main import ( "context" + "io/ioutil" "os" "path" - - "github.com/joho/godotenv" + "strings" "srs-proxy/errors" "srs-proxy/logger" @@ -16,14 +16,41 @@ import ( // loadEnvFile loads the environment variables from file. Note that we only use .env file. func loadEnvFile(ctx context.Context) error { - if workDir, err := os.Getwd(); err != nil { + workDir, err := os.Getwd() + if err != nil { return errors.Wrapf(err, "getpwd") - } else { - envFile := path.Join(workDir, ".env") - if _, err := os.Stat(envFile); err == nil { - if err := godotenv.Load(envFile); err != nil { - return errors.Wrapf(err, "load %v", envFile) + } + + envFile := path.Join(workDir, ".env") + if _, err := os.Stat(envFile); err != nil { + return nil + } + + file, err := os.Open(envFile) + if err != nil { + return errors.Wrapf(err, "open %v", envFile) + } + defer file.Close() + + b, err := ioutil.ReadAll(file) + if err != nil { + return errors.Wrapf(err, "read %v", envFile) + } + + lines := strings.Split(strings.Replace(string(b), "\r\n", "\n", -1), "\n") + for _, line := range lines { + if strings.HasPrefix(strings.TrimSpace(line), "#") { + continue + } + + if pos := strings.IndexByte(line, '='); pos > 0 { + key := strings.TrimSpace(line[:pos]) + value := strings.TrimSpace(line[pos+1:]) + if v := os.Getenv(key); v != "" { + continue } + + os.Setenv(key, value) } } diff --git a/proxy/go.mod b/proxy/go.mod index 2e2a17ab34..e9e196d2f7 100644 --- a/proxy/go.mod +++ b/proxy/go.mod @@ -2,10 +2,7 @@ module srs-proxy go 1.18 -require ( - github.com/go-redis/redis/v8 v8.11.5 - github.com/joho/godotenv v1.5.1 -) +require github.com/go-redis/redis/v8 v8.11.5 require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect diff --git a/proxy/go.sum b/proxy/go.sum index 1efc5318ed..7342ff813f 100644 --- a/proxy/go.sum +++ b/proxy/go.sum @@ -5,8 +5,6 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=