29 lines
482 B
Go
29 lines
482 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
func NewDBConnection(lc fx.Lifecycle) *pgxpool.Pool {
|
|
pool, err := pgxpool.New(context.Background(), "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable");
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
if err := pool.Ping(context.Background()); err != nil {
|
|
pool.Close()
|
|
panic(err.Error())
|
|
}
|
|
|
|
lc.Append(
|
|
fx.StopHook(func() {
|
|
pool.Close()
|
|
}),
|
|
)
|
|
|
|
return pool
|
|
}
|