directory/internal/database/database.go

127 lines
2.5 KiB
Go
Raw Normal View History

2024-06-01 22:39:55 +00:00
package database
import (
"context"
"database/sql"
2024-06-01 23:00:14 +00:00
"errors"
2024-06-01 22:39:55 +00:00
"github.com/rs/zerolog"
"go.uber.org/fx"
2024-06-01 23:00:14 +00:00
"gorm.io/driver/postgres"
2024-06-01 22:39:55 +00:00
"gorm.io/gorm"
"sneak.berlin/go/directory/internal/config"
"sneak.berlin/go/directory/internal/globals"
"sneak.berlin/go/directory/internal/logger"
2024-06-01 23:00:14 +00:00
2024-06-01 22:39:55 +00:00
_ "github.com/joho/godotenv/autoload"
2024-06-15 14:41:37 +00:00
_ "github.com/lib/pq" // Import the PostgreSQL driver
2024-06-01 22:39:55 +00:00
)
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 = &params
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")
2024-06-01 23:00:14 +00:00
err := s.Connect()
if err != nil {
return err
}
2024-06-01 22:39:55 +00:00
return nil
},
OnStop: func(ctx context.Context) error {
2024-06-01 23:00:14 +00:00
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")
2024-06-01 22:39:55 +00:00
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 {
2024-06-01 23:07:34 +00:00
dsn := d.params.Config.DBDSN
2024-06-01 23:00:14 +00:00
if dsn == "" {
err := errors.New("database DSN is empty")
d.log.Error().Err(err).Msg("failed to get database DSN from config")
2024-06-01 22:39:55 +00:00
return err
}
2024-06-01 23:00:14 +00:00
2024-06-01 22:39:55 +00:00
d.log.Info().
2024-06-01 23:00:14 +00:00
Str("dsn", dsn).
Msg("connecting to database")
2024-06-01 22:39:55 +00:00
2024-06-01 23:00:14 +00:00
sqlDB, err := sql.Open("postgres", dsn)
2024-06-01 22:39:55 +00:00
if err != nil {
d.log.Error().Err(err).Msg("failed to open database")
return err
}
d.SQLDB = sqlDB
2024-06-01 23:00:14 +00:00
// 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{})
2024-06-01 22:39:55 +00:00
if err != nil {
2024-06-01 23:00:14 +00:00
d.log.Error().Err(err).Msg("failed to connect database")
2024-06-01 22:39:55 +00:00
return err
}
d.DB = db
return nil
}
2024-06-01 23:00:14 +00:00
func (d *Database) Ping() error {
err := d.SQLDB.Ping()
if err != nil {
return err
}
d.log.Info().Msg("successfully pinged the database")
return nil
}