szoe-pontok/database/connection.go
2024-10-10 20:22:03 +02:00

25 lines
504 B
Go

package database
import (
"fmt"
"git.tek.govt.hu/dowerx/szoe-pontok/config"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
var db *sqlx.DB
func GetDB() *sqlx.DB {
if db == nil {
cfg := config.GetConfig()
newDB, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Database.Host, cfg.Database.Port, cfg.Database.User, cfg.Database.Password, cfg.Database.DBname))
if err != nil {
panic(err)
}
db = newDB
}
return db
}