- Go backend skeleton: config (caarlos0/env), slog JSON logging, pgxpool store, HTTP server with graceful shutdown. - httpapi: render helpers, Problem errors, middleware chain (requestID / logging / recover / CORS), /healthz and /readyz. - Migrations: full initial schema (users, sessions, lists, list_members, items, op_log SOURCE OF TRUTH, item_names) + golang-migrate runner binary using source/iofs (embedded). - Docker: multi-stage Dockerfile (Go 1.26 -> distroless nonroot), builds both server and migrate binaries. - deploy: docker-compose (caddy + backend + migrate + postgres:16), Caddyfile (auto-HTTPS), .env.example, pg extensions init script. - AGENTS.md: project context + roadmap for AI agents. Verified: image builds, both binaries run in container (smoke test).
32 lines
842 B
Go
32 lines
842 B
Go
// Package store wraps database access (pgxpool) for the backend.
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/mitbringsl/backend/internal/config"
|
|
)
|
|
|
|
// New creates and configures a pgx connection pool, then pings the DB to verify access.
|
|
func New(ctx context.Context, cfg *config.Config) (*pgxpool.Pool, error) {
|
|
pcfg, err := pgxpool.ParseConfig(cfg.DatabaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse database url: %w", err)
|
|
}
|
|
pcfg.MaxConns = cfg.DBMaxOpenConns
|
|
pcfg.MinConns = cfg.DBMaxIdleConns
|
|
pcfg.MaxConnLifetime = cfg.DBMaxLifetime
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, pcfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping db: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|