Skip to content

Commit ed6d0d6

Browse files
committed
Better error on not existing .env file and eliminate init on the process
1 parent 1ee24c3 commit ed6d0d6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

database/db.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var Db *gorm.DB
1919

2020
func InitDb() *gorm.DB {
2121
Db = connectDB()
22+
if Db == nil {
23+
return nil
24+
}
2225

2326
err := createRoot()
2427
if err != nil {
@@ -37,7 +40,8 @@ func connectDB() *gorm.DB {
3740
//load .env file
3841
err := godotenv.Load()
3942
if err != nil {
40-
fmt.Println(err)
43+
fmt.Println(".env file was not found. You should add a .env file on project root with:\nDB_USERNAME \nDB_PASSWORD \nDB_NAME \nDB_HOST \nDB_PORT \nSECRET")
44+
return nil
4145
}
4246

4347
//get .env variables

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"github.com/Darklabel91/API_Names/routes"
77
)
88

9-
func init() {
10-
database.InitDb()
11-
}
12-
139
func main() {
10+
r := database.InitDb()
11+
if r == nil {
12+
return
13+
}
14+
1415
fmt.Println("- live")
1516
routes.HandleRequests()
1617
}

0 commit comments

Comments
 (0)