Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load env config #42

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package main
import (
"fmt"
"log"
"ngodeyuk-core/database"
"ngodeyuk-core/internal/infrastructure/routes"
"ngodeyuk-core/pkg/utils"
"path/filepath"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"

"ngodeyuk-core/database"
"ngodeyuk-core/internal/infrastructure/routes"
)

func init() {
utils.LoadEnv()
}

func main() {
route := gin.Default()
config := cors.DefaultConfig()
Expand All @@ -22,11 +25,6 @@ func main() {

route.Use(cors.New(config))

err := godotenv.Load()
if err != nil {
log.Fatalf("error load .env file: %v", err)
}

db, err := database.InitDB()
if err != nil {
panic("failed to connect database.")
Expand Down
6 changes: 0 additions & 6 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import (
"log"
"os"

"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func InitDB() (*gorm.DB, error) {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}

dsn := "host=" + os.Getenv("DB_HOST") +
" user=" + os.Getenv("DB_USER") +
" password=" + os.Getenv("DB_PASSWORD") +
Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/load_env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package utils

import (
"log"

"github.com/joho/godotenv"
)

func LoadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
Loading