aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/main.go')
-rw-r--r--backend/main.go22
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()