1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Proxy: Remove dependency of godotenv. #4158

This commit is contained in:
winlin 2024-09-10 10:53:09 +08:00
parent 2e4014ae1c
commit e674f8266a
3 changed files with 36 additions and 14 deletions

View file

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