Skip to content

Commit

Permalink
Fix fatal error if .env doesn't load in production
Browse files Browse the repository at this point in the history
  • Loading branch information
davidianstyle committed Feb 27, 2024
1 parent a557d40 commit a311871
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENVIRONMENT=[development]
MYSQL_PORT=[3306]
DB_USER=[root]
DB_PASSWORD=[password]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RESTful API for interacting with David Chang programmatically
### Configure
`$ cp .env.example .env`
- Fill out all the details for your local database (replace examples in brackets):
- ENVIRONMENT=[development]
- MYSQL_PORT=[3306]
- DB_USER=[root]
- DB_PASSWORD=[password]
Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ type Resume struct {
}

func init() {
// Load environment variables from a file if needed
// For simplicity, you can set them directly in your Cloud Run service configuration.
// LoadEnvFromFile(".env") // Uncomment this line if you are using a .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
// Check if running in a development environment
env := os.Getenv("ENVIRONMENT")
if env == "" || strings.ToLower(env) == "development" {
// Load environment variables from a file for development
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
} else {
// In production, environment variables are provided by the deployment platform (ie. Cloud Run)
}
}

Expand Down

0 comments on commit a311871

Please sign in to comment.