46 lines
907 B
Go
46 lines
907 B
Go
package config
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func LoadEnv() {
|
|
paths := []string{
|
|
".env.local",
|
|
filepath.Join("server", ".env.local"),
|
|
}
|
|
for _, p := range paths {
|
|
if loadFile(p) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadFile(path string) bool {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer f.Close()
|
|
s := bufio.NewScanner(f)
|
|
for s.Scan() {
|
|
line := strings.TrimSpace(s.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
idx := strings.Index(line, "=")
|
|
if idx <= 0 {
|
|
continue
|
|
}
|
|
k := strings.TrimSpace(line[:idx])
|
|
v := strings.TrimSpace(line[idx+1:])
|
|
if _, ok := os.LookupEnv(k); !ok || os.Getenv(k) == "" {
|
|
os.Setenv(k, v)
|
|
}
|
|
}
|
|
return true
|
|
}
|