Skip to content

Commit

Permalink
Improve env file handling (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Oct 12, 2024
1 parent 06a47bb commit e93243e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 23 additions & 13 deletions runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit e93243e

Please sign in to comment.