szoe-pontok/database/connection.go

43 lines
829 B
Go
Raw Permalink Normal View History

2024-10-10 18:22:03 +00:00
package database
import (
2024-10-10 19:41:49 +00:00
"context"
2024-10-10 18:22:03 +00:00
"fmt"
"git.tek.govt.hu/dowerx/szoe-pontok/config"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
2024-10-10 19:41:49 +00:00
"github.com/redis/go-redis/v9"
2024-10-10 18:22:03 +00:00
)
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
}
2024-10-10 19:41:49 +00:00
var rdb *redis.Client
var ctx context.Context
func GetRDB() (*redis.Client, context.Context) {
if rdb == nil {
ctx = context.Background()
rdb = redis.NewClient(&config.GetConfig().Redis)
if err := rdb.Ping(ctx).Err(); err != nil {
panic(err)
}
}
return rdb, ctx
}