Skip to content

Commit d4d186a

Browse files
authored
Merge pull request #526 from Glimesh/allow-no-env-startup
Allow startup without .env files
2 parents 2f3de2d + 7b7bd95 commit d4d186a

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

internal/environment/environment.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,11 @@ func LoadEnvironmentVariables() {
4444

4545
func loadConfigs() error {
4646
if os.Getenv(appEnv) == "development" {
47-
log.Println("Environment: Loading `" + envFileDevelopment + "`")
48-
if err := godotenv.Load(envFileDevelopment); err != nil {
49-
log.Printf("Environment: Could not load `%s`: %v", envFileDevelopment, err)
50-
}
51-
return nil
47+
return loadOptionalEnvironmentFile(envFileDevelopment)
5248
}
5349

54-
log.Println("Environment: Loading `" + envFileProduction + "`")
55-
if err := godotenv.Load(envFileProduction); err != nil {
56-
log.Printf("Environment: Could not load `%s`: %v", envFileProduction, err)
50+
if err := loadOptionalEnvironmentFile(envFileProduction); err != nil {
51+
return err
5752
}
5853

5954
if os.Getenv(FrontendDisabled) == "" {
@@ -67,6 +62,22 @@ func loadConfigs() error {
6762
return nil
6863
}
6964

65+
func loadOptionalEnvironmentFile(fileName string) error {
66+
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
67+
log.Printf("Environment: `%s` not found, continuing with system environment", fileName)
68+
return nil
69+
} else if err != nil {
70+
return err
71+
}
72+
73+
log.Println("Environment: Loading `" + fileName + "`")
74+
if err := godotenv.Load(fileName); err != nil {
75+
return err
76+
}
77+
78+
return nil
79+
}
80+
7081
func GetFrontendPath() string {
7182
frontendPath := os.Getenv(frontendPath)
7283
if frontendPath == "" {

0 commit comments

Comments
 (0)