2b37986218
Small Go tool + distroless container used as a K8s initContainer to block an app until its database (Postgres/MySQL) is reachable. Env-var configured (WAITFORDB_* + libpq PG* fallback), configurable timeout/interval, redacted logs, exit codes. Woodpecker CI publishes docker-internal/waitfordb on tag.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
// Package driver abstracts the per-database connection details behind a small
|
|
// interface so new engines can be added without touching the wait loop.
|
|
package driver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.unkin.net/unkin/waitfordb/internal/config"
|
|
)
|
|
|
|
// Pinger is a live handle to a database that can answer a trivial liveness
|
|
// query. A successful Ping proves the server is up, auth succeeded, and the
|
|
// target database/role exist.
|
|
type Pinger interface {
|
|
Ping(ctx context.Context) error
|
|
Close() error
|
|
}
|
|
|
|
// Driver knows how to open a Pinger for one database engine.
|
|
type Driver interface {
|
|
Name() string
|
|
Open(cfg config.Config) (Pinger, error)
|
|
}
|
|
|
|
var registry = map[string]Driver{}
|
|
|
|
func register(d Driver) { registry[d.Name()] = d }
|
|
|
|
// Get returns the registered driver for name.
|
|
func Get(name string) (Driver, error) {
|
|
d, ok := registry[strings.ToLower(name)]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unsupported driver %q (supported: %s)", name, strings.Join(Names(), ", "))
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// Names lists the registered driver names, sorted.
|
|
func Names() []string {
|
|
out := make([]string, 0, len(registry))
|
|
for n := range registry {
|
|
out = append(out, n)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|