Skip to content

Commit

Permalink
Proxy: Remove dependency of godotenv. #4158
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 10, 2024
1 parent 2e4014a commit e674f82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
43 changes: 35 additions & 8 deletions proxy/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,52 @@ package main

import (
"context"
"io/ioutil"
"os"
"path"

"github.com/joho/godotenv"
"strings"

"srs-proxy/errors"
"srs-proxy/logger"
)

// 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)
}
}

Expand Down
5 changes: 1 addition & 4 deletions proxy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions proxy/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit e674f82

Please sign in to comment.