package database import ( "context" "database/sql" "errors" "github.com/rs/zerolog" "go.uber.org/fx" "gorm.io/driver/postgres" "gorm.io/gorm" "sneak.berlin/go/directory/internal/config" "sneak.berlin/go/directory/internal/globals" "sneak.berlin/go/directory/internal/logger" _ "github.com/joho/godotenv/autoload" ) type DatabaseParams struct { fx.In Logger *logger.Logger Config *config.Config Globals *globals.Globals } type Database struct { URL string log *zerolog.Logger params *DatabaseParams DB *gorm.DB SQLDB *sql.DB } func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) { s := new(Database) s.params = ¶ms s.log = params.Logger.Get() s.log.Info().Msg("Database instantiated") lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { s.log.Info().Msg("Database OnStart Hook") err := s.Connect() if err != nil { return err } return nil }, OnStop: func(ctx context.Context) error { if s.SQLDB != nil { err := s.SQLDB.Close() if err != nil { s.log.Error().Err(err).Msg("failed to close database connection") return err } } s.log.Info().Msg("Database connection closed") return nil }, }) return s, nil } func (d *Database) Close() { d.log.Info().Msg("Database Close()") } func (d *Database) Get() (*gorm.DB, error) { if d.DB == nil { err := d.Connect() if err != nil { d.log.Error().Err(err).Msg("failed to connect to database") return nil, err } } return d.DB, nil } func (d *Database) Connect() error { dsn := d.params.Config.DBDSN if dsn == "" { err := errors.New("database DSN is empty") d.log.Error().Err(err).Msg("failed to get database DSN from config") return err } d.log.Info(). Str("dsn", dsn). Msg("connecting to database") sqlDB, err := sql.Open("postgres", dsn) if err != nil { d.log.Error().Err(err).Msg("failed to open database") return err } d.SQLDB = sqlDB // Ping the database to ensure the connection is valid err = d.Ping() if err != nil { d.log.Error().Err(err).Msg("failed to ping database") return err } db, err := gorm.Open(postgres.New(postgres.Config{ Conn: d.SQLDB, }), &gorm.Config{}) if err != nil { d.log.Error().Err(err).Msg("failed to connect database") return err } d.DB = db return nil } func (d *Database) Ping() error { err := d.SQLDB.Ping() if err != nil { return err } d.log.Info().Msg("successfully pinged the database") return nil }