diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-29 01:52:56 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-29 02:14:23 +0900 |
| commit | 0b90018afbd438d61db7b41e5c3ea41cbb563bfe (patch) | |
| tree | 758a590fefab742c1e276ab58be123d23b94b36c /backend/main.go | |
| parent | fb31ac445245f26c10a38ac6ac60916bfbbca976 (diff) | |
| download | iosdc-japan-2025-albatross-0b90018afbd438d61db7b41e5c3ea41cbb563bfe.tar.gz iosdc-japan-2025-albatross-0b90018afbd438d61db7b41e5c3ea41cbb563bfe.tar.zst iosdc-japan-2025-albatross-0b90018afbd438d61db7b41e5c3ea41cbb563bfe.zip | |
feat(backend): use connection pool
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/backend/main.go b/backend/main.go index 379fe8d..8042674 100644 --- a/backend/main.go +++ b/backend/main.go @@ -6,7 +6,7 @@ import ( "log" "net/http" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" oapimiddleware "github.com/oapi-codegen/echo-middleware" @@ -16,6 +16,19 @@ import ( "github.com/nsfisis/iosdc-2024-albatross/backend/game" ) +func connectDB(ctx context.Context, dsn string) (*pgxpool.Pool, error) { + pool, err := pgxpool.New(ctx, dsn) + if err != nil { + return nil, err + } + + if err := pool.Ping(ctx); err != nil { + return nil, err + } + + return pool, nil +} + func main() { var err error config, err := NewConfigFromEnv() @@ -30,13 +43,14 @@ func main() { ctx := context.Background() - conn, err := pgx.Connect(ctx, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", config.dbHost, config.dbPort, config.dbUser, config.dbPassword, config.dbName)) + dbDSN := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", config.dbHost, config.dbPort, config.dbUser, config.dbPassword, config.dbName) + connPool, err := connectDB(ctx, dbDSN) if err != nil { log.Fatalf("Error connecting to db %v", err) } - defer conn.Close(ctx) + defer connPool.Close() - queries := db.New(conn) + queries := db.New(connPool) e := echo.New() |
