Monorepo com apps/api (Go 1.23 + chi + pgx) e apps/web (Vue 3 + Vite + TypeScript). Dockerfile multi-stage consolida tudo em imagem única (~25MB) para deploy no Dokploy. Migrations rodando na inicialização; Vue embedado no binário Go via //go:embed. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
29 lines
582 B
Go
29 lines
582 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func Connect(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
|
var pool *pgxpool.Pool
|
|
var err error
|
|
|
|
for i := range 10 {
|
|
pool, err = pgxpool.New(ctx, dsn)
|
|
if err == nil {
|
|
if pingErr := pool.Ping(ctx); pingErr == nil {
|
|
return pool, nil
|
|
}
|
|
pool.Close()
|
|
}
|
|
wait := time.Duration(i+1) * 500 * time.Millisecond
|
|
fmt.Printf("db not ready, retrying in %s...\n", wait)
|
|
time.Sleep(wait)
|
|
}
|
|
return nil, fmt.Errorf("could not connect to database after retries: %w", err)
|
|
}
|