diff --git a/compose.dev.yml b/compose.dev.yml index ce1841b..075b425 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -5,4 +5,32 @@ services: - "8080:8080" environment: - WEBHOOK_SECRET=${WEBHOOK_SECRET:-} + - DATABASE_URL=${DATABASE_URL:-} restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + required: false + + postgres: + image: postgres:17-alpine + profiles: + - postgres + ports: + - "5432:5432" + environment: + POSTGRES_USER: ${POSTGRES_USER:-diun} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-diun} + POSTGRES_DB: ${POSTGRES_DB:-diundashboard} + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-diun}"] + interval: 5s + timeout: 5s + retries: 5 + start_period: 10s + restart: unless-stopped + +volumes: + postgres-data: diff --git a/compose.yml b/compose.yml index d25799f..678f519 100644 --- a/compose.yml +++ b/compose.yml @@ -1,3 +1,4 @@ +# Minimum Docker Compose v2.20 required for depends_on.required services: app: image: gitea.jeanlucmakiola.de/makiolaj/diundashboard:latest @@ -7,9 +8,33 @@ services: - WEBHOOK_SECRET=${WEBHOOK_SECRET:-} - PORT=${PORT:-8080} - DB_PATH=/data/diun.db + - DATABASE_URL=${DATABASE_URL:-} volumes: - diun-data:/data restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + required: false + + postgres: + image: postgres:17-alpine + profiles: + - postgres + environment: + POSTGRES_USER: ${POSTGRES_USER:-diun} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-diun} + POSTGRES_DB: ${POSTGRES_DB:-diundashboard} + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-diun}"] + interval: 5s + timeout: 5s + retries: 5 + start_period: 10s + restart: unless-stopped volumes: diun-data: + postgres-data: diff --git a/pkg/diunwebhook/postgres_test.go b/pkg/diunwebhook/postgres_test.go new file mode 100644 index 0000000..568029e --- /dev/null +++ b/pkg/diunwebhook/postgres_test.go @@ -0,0 +1,29 @@ +//go:build postgres + +package diunwebhook + +import ( + "database/sql" + "os" + + _ "github.com/jackc/pgx/v5/stdlib" +) + +// NewTestPostgresServer constructs a Server backed by a PostgreSQL database. +// Requires a running PostgreSQL instance. Set TEST_DATABASE_URL to override +// the default connection string. +func NewTestPostgresServer() (*Server, error) { + databaseURL := os.Getenv("TEST_DATABASE_URL") + if databaseURL == "" { + databaseURL = "postgres://diun:diun@localhost:5432/diundashboard_test?sslmode=disable" + } + db, err := sql.Open("pgx", databaseURL) + if err != nil { + return nil, err + } + if err := RunPostgresMigrations(db); err != nil { + return nil, err + } + store := NewPostgresStore(db) + return NewServer(store, ""), nil +}