112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type postgresStore struct {
|
|
configured bool
|
|
psqlPath string
|
|
env []string
|
|
}
|
|
|
|
func newPostgresStore() (*postgresStore, error) {
|
|
raw := strings.TrimSpace(os.Getenv("DATABASE_URL"))
|
|
if raw == "" {
|
|
return &postgresStore{}, nil
|
|
}
|
|
path, err := exec.LookPath("psql")
|
|
if err != nil {
|
|
return nil, errors.New("DATABASE_URL ist gesetzt, aber psql fehlt; installiere postgresql-client")
|
|
}
|
|
env, err := postgresEnv(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
store := &postgresStore{configured: true, psqlPath: path, env: append(os.Environ(), env...)}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if _, err := store.exec(ctx, "SELECT 1;"); err != nil {
|
|
return nil, fmt.Errorf("PostgreSQL nicht erreichbar: %w", err)
|
|
}
|
|
return store, nil
|
|
}
|
|
|
|
func postgresEnv(raw string) ([]string, error) {
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if u.Scheme != "postgres" && u.Scheme != "postgresql" {
|
|
return nil, errors.New("DATABASE_URL muss postgres:// oder postgresql:// verwenden")
|
|
}
|
|
if u.User == nil || u.User.Username() == "" {
|
|
return nil, errors.New("DATABASE_URL enthält keinen Benutzer")
|
|
}
|
|
db := strings.TrimPrefix(u.Path, "/")
|
|
if db == "" {
|
|
return nil, errors.New("DATABASE_URL enthält keine Datenbank")
|
|
}
|
|
env := []string{"PGUSER=" + u.User.Username(), "PGDATABASE=" + db, "PGOPTIONS=-c search_path=landwelle,public", "PGAPPNAME=landwelle-portal"}
|
|
if p, ok := u.User.Password(); ok {
|
|
env = append(env, "PGPASSWORD="+p)
|
|
}
|
|
if h := u.Hostname(); h != "" {
|
|
env = append(env, "PGHOST="+h)
|
|
}
|
|
if p := u.Port(); p != "" {
|
|
env = append(env, "PGPORT="+p)
|
|
}
|
|
if v := u.Query().Get("sslmode"); v != "" {
|
|
env = append(env, "PGSSLMODE="+v)
|
|
}
|
|
return env, nil
|
|
}
|
|
|
|
func (p *postgresStore) Enabled() bool { return p != nil && p.configured }
|
|
func (p *postgresStore) Ping(ctx context.Context) error {
|
|
if !p.Enabled() {
|
|
return errors.New("PostgreSQL ist nicht konfiguriert")
|
|
}
|
|
_, err := p.exec(ctx, "SELECT 1;")
|
|
return err
|
|
}
|
|
func (p *postgresStore) exec(ctx context.Context, sql string) ([]byte, error) {
|
|
if !p.Enabled() {
|
|
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
|
}
|
|
cmd := exec.CommandContext(ctx, p.psqlPath, "--no-psqlrc", "--set", "ON_ERROR_STOP=1", "--tuples-only", "--no-align", "--command", sql)
|
|
cmd.Env = p.env
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (p *postgresStore) queryJSON(ctx context.Context, sql string, dest any) error {
|
|
out, err := p.exec(ctx, sql)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
raw := strings.TrimSpace(string(out))
|
|
if raw == "" {
|
|
raw = "[]"
|
|
}
|
|
return json.Unmarshal([]byte(raw), dest)
|
|
}
|
|
func sqlText(s string) string { return "'" + strings.ReplaceAll(s, "'", "''") + "'" }
|
|
func sqlBool(v bool) string {
|
|
if v {
|
|
return "true"
|
|
}
|
|
return "false"
|
|
}
|