Initial commit
This commit is contained in:
28
backend/internal/db/connection.go
Normal file
28
backend/internal/db/connection.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user