Add env parsing

This commit is contained in:
2024-10-15 23:34:46 +02:00
parent 38c7352675
commit 895d6776b8
5 changed files with 79 additions and 0 deletions

30
backend/utils.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
func LoadEnv(fpath string) (map[string]string, error) {
res := make(map[string]string)
fpath = filepath.Clean(fpath)
log.Printf("Trying to open env file at '%s'", fpath)
file, err := os.Open(fpath)
if err != nil {
return res, fmt.Errorf("error opening env file: %w", err)
}
defer file.Close()
res, err = godotenv.Parse(file)
if err != nil {
return res, fmt.Errorf("error parsing env file: %w", err)
}
return res, nil
}