Use a connection pool for Postgres
This commit is contained in:
parent
99cd70165e
commit
ffb19acef7
3 changed files with 15 additions and 6 deletions
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -16,6 +16,7 @@
|
|||
"congstar",
|
||||
"datetime",
|
||||
"dbname",
|
||||
"dbpool",
|
||||
"DBSTRING",
|
||||
"easybox",
|
||||
"Eo's",
|
||||
|
|
@ -55,6 +56,7 @@
|
|||
"PGPORT",
|
||||
"pgtype",
|
||||
"PGUSER",
|
||||
"pgxpool",
|
||||
"Prés",
|
||||
"pressly",
|
||||
"psql",
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -12,11 +12,13 @@ require (
|
|||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mmcdole/goxpp v1.1.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
golang.org/x/crypto v0.17.0 // indirect
|
||||
golang.org/x/net v0.10.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
||||
|
|
|
|||
15
main.go
15
main.go
|
|
@ -7,11 +7,11 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/TheLovinator1/FeedVault/db"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
conn *pgx.Conn
|
||||
dbpool *pgxpool.Pool
|
||||
DB *db.Queries
|
||||
)
|
||||
|
||||
|
|
@ -21,16 +21,21 @@ func init() {
|
|||
ctx := context.Background()
|
||||
|
||||
// Open a database connection
|
||||
conn, err := pgx.Connect(ctx, "postgresql://localhost/feedvault?user=feedvault&password=feedvault")
|
||||
dbpool, err := pgxpool.New(ctx, "postgresql://localhost/feedvault?user=feedvault&password=feedvault")
|
||||
if err != nil {
|
||||
log.Fatalf("pgx.Connect(): %v", err)
|
||||
}
|
||||
|
||||
DB = db.New(conn)
|
||||
DB = db.New(dbpool)
|
||||
|
||||
// Test the connection
|
||||
if err := dbpool.Ping(ctx); err != nil {
|
||||
log.Fatalf("dbpool.Ping(): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer conn.Close(context.Background())
|
||||
defer dbpool.Close()
|
||||
|
||||
log.Print("Starting server")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue