Files
aresv2/database/config/postgres_db.go
Beyhan Oğur 4362c3b83f first commit
2026-04-26 21:33:39 +03:00

47 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package database
import (
configs "ares/config"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var DBPg *gorm.DB
func ConnectDBPg() {
dsn := configs.AppConfig.DBPGUrl
if dsn == "" {
if configs.Logger != nil {
configs.Logger.Warn(".env dosyasında DB_URL ayarlı değil — veritabanı bağlantısı atlanıyor (geliştirme modu)")
}
return
}
if configs.Logger != nil {
configs.Logger.Info("Yapılandırmada DB_URL_PG bulundu, veritabanına bağlanılmaya çalışılıyor...")
}
// GORM için MySQL konfigürasyonu
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Warn), // Info seviyesi (performans etkileyebilir); üretimde Error seviyesine alınabilir
PrepareStmt: true, // PrepareStmt performansını artırmak için
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
if err != nil {
if configs.Logger != nil {
configs.Logger.Sugar().Errorf("Postgres veritabanı bağlantısı kurulamadı: %v", err)
}
return
}
if configs.Logger != nil {
configs.Logger.Info("Postgres veritabanı bağlantısı kuruldu.")
}
DBPg = db
}