Skip to content

Commit 7efe35a

Browse files
committed
Use APP_ENV when choosing what .env file to load
Default to .env.production to keep existing behavior
1 parent 1173452 commit 7efe35a

2 files changed

Lines changed: 39 additions & 28 deletions

File tree

internal/environment/environment.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,62 @@
11
package environment
22

33
import (
4+
"errors"
45
"log"
56
"os"
67
"path/filepath"
78

89
"github.com/joho/godotenv"
910
)
1011

12+
const (
13+
envFileDevelopment = ".env.development"
14+
envFileProduction = ".env.production"
15+
)
16+
17+
var errNoBuildDirectory = errors.New("build directory does not exist, run `npm install` and `npm run build` in the web directory")
18+
1119
func LoadEnvironmentVariables() {
12-
files := []string{
13-
".env.development",
14-
".env.production",
15-
}
20+
if err := loadConfigs(); err != nil {
21+
if errors.Is(err, errNoBuildDirectory) {
22+
log.Fatal("Environment:", err)
23+
}
1624

17-
// Load base environment file if available
18-
loadEnvironmentFile(".env")
25+
log.Println("Environment: Failed to find config in CWD, changing CWD to executable path")
1926

20-
for _, file := range files {
21-
loadEnvironmentFile(file)
22-
setDefaultEnvironmentVariables()
23-
return
24-
}
27+
executablePath, executableErr := os.Executable()
28+
if executableErr != nil {
29+
log.Fatal("Environment:", executableErr)
30+
}
2531

26-
log.Println("Environment: Could not find any environment files")
27-
os.Exit(0)
28-
}
32+
if chdirErr := os.Chdir(filepath.Dir(executablePath)); chdirErr != nil {
33+
log.Fatal("Environment:", chdirErr)
34+
}
2935

30-
func loadEnvironmentFile(filePath string) {
31-
currentWorkingDirectory, err := os.Getwd()
32-
if err != nil {
33-
log.Fatal("Environment:", err)
36+
if retryErr := loadConfigs(); retryErr != nil {
37+
log.Fatal("Environment:", retryErr)
38+
}
3439
}
3540

36-
path := filepath.Join(currentWorkingDirectory, filePath)
37-
38-
if _, err := os.Stat(path); err == nil {
39-
err := godotenv.Overload(path)
41+
setDefaultEnvironmentVariables()
42+
}
4043

41-
if err != nil {
42-
log.Println("Environment: Error occurred loading environment file", path)
43-
log.Println(err)
44+
func loadConfigs() error {
45+
if os.Getenv(APP_ENV) == "development" {
46+
log.Println("Environment: Loading `" + envFileDevelopment + "`")
47+
return godotenv.Load(envFileDevelopment)
48+
}
4449

45-
os.Exit(0)
46-
}
50+
log.Println("Environment: Loading `" + envFileProduction + "`")
51+
if err := godotenv.Load(envFileProduction); err != nil {
52+
return err
53+
}
4754

48-
log.Println("Environment: Loaded", filePath)
55+
if _, err := os.Stat("./web/build"); os.IsNotExist(err) && os.Getenv(FRONTEND_DISABLED) == "" {
56+
return errNoBuildDirectory
4957
}
58+
59+
return nil
5060
}
5161

5262
func setDefaultEnvironmentVariables() {

internal/environment/variables.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package environment
22

33
const (
44
// SERVER
5+
APP_ENV = "APP_ENV"
56
HTTP_ADDRESS = "HTTP_ADDRESS"
67
HTTPS_REDIRECT_PORT = "HTTPS_REDIRECT_PORT"
78
HTTP_ENABLE_REDIRECT = "ENABLE_HTTP_REDIRECT"

0 commit comments

Comments
 (0)