diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d109c69..ce1fc09d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ In previous releases, the name "Hypermode" was used for all three._ - Remove `go generate` and fix docker build [#455](https://github.com/hypermodeinc/modus/pull/455) - Remove AWS Secrets Manager client [#456](https://github.com/hypermodeinc/modus/pull/456) - Make app path required [#457](https://github.com/hypermodeinc/modus/pull/457) +- Improve `.env` file handling [#458](https://github.com/hypermodeinc/modus/pull/458) ## 2024-10-02 - Version 0.12.7 diff --git a/runtime/main.go b/runtime/main.go index b34d090a..e2ac785f 100644 --- a/runtime/main.go +++ b/runtime/main.go @@ -21,8 +21,8 @@ import ( "github.com/hypermodeinc/modus/runtime/middleware" "github.com/hypermodeinc/modus/runtime/services" "github.com/hypermodeinc/modus/runtime/utils" - "github.com/joho/godotenv" + "github.com/rs/zerolog" ) func main() { @@ -37,18 +37,8 @@ func main() { Str("environment", config.GetEnvironmentName()). Msg("Starting Modus Runtime.") - // Load environment variables from plugins path - // note: .env file is optional, so don't log if it's not found - err := godotenv.Load(filepath.Join(config.AppPath, ".env")) - if err != nil && !os.IsNotExist(err) { - log.Warn().Err(err).Msg("Error reading .env file. Ignoring.") - } - if config.IsDevEnvironment() { - err = godotenv.Load(filepath.Join(config.AppPath, ".env.local")) - if err != nil && !os.IsNotExist(err) { - log.Warn().Err(err).Msg("Error reading .env.local file. Ignoring.") - } - } + // Load environment variables from .env file(s) + loadEnvFiles(log) // Initialize Sentry rootSourcePath := getRootSourcePath() @@ -70,6 +60,26 @@ func main() { httpserver.Start(ctx, local) } +func loadEnvFiles(log *zerolog.Logger) { + envName := config.GetEnvironmentName() + + files := []string{ + ".env." + envName + ".local", + ".env." + envName, + ".env.local", + ".env", + } + + for _, file := range files { + path := filepath.Join(config.AppPath, file) + if _, err := os.Stat(path); err == nil { + if err := godotenv.Load(path); err != nil { + log.Warn().Err(err).Msgf("Failed to load %s file.", file) + } + } + } +} + func getRootSourcePath() string { _, filename, _, ok := runtime.Caller(0) if !ok {