Files
uptimesebi/internal/platform/run.go
T
Sebastian UnterschützandClaude Opus 5.5 d20225982a refactor: follow the google go style guide
Document every package and exported identifier, return the log level
from NewLogger instead of keeping it in a package variable, and stop
tests from mutating the argon2 parameters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 00:18:02 +01:00

82 lines
2.2 KiB
Go

package platform
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
)
// Deps are the connections every command needs.
type Deps struct {
Log *slog.Logger
DB *pgxpool.Pool
Redis *redis.Client
}
// Close releases all connections. It is safe on a partially built Deps.
func (d *Deps) Close() {
if d.DB != nil {
d.DB.Close()
}
if d.Redis != nil {
_ = d.Redis.Close()
}
}
// Probes returns readiness probes for the connections.
func (d *Deps) Probes() map[string]Probe {
return map[string]Probe{
"postgres": func(ctx context.Context) error { return d.DB.Ping(ctx) },
"redis": func(ctx context.Context) error { return d.Redis.Ping(ctx).Err() },
}
}
// SignalContext is cancelled on SIGINT or SIGTERM.
func SignalContext() (context.Context, context.CancelFunc) {
return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
}
// Connect validates c, refuses to start against an outdated schema, and
// opens Postgres and Redis.
func Connect(ctx context.Context, c config.Common, log *slog.Logger) (*Deps, error) {
if err := c.Validate(); err != nil {
return nil, err
}
pending, err := migrations.Pending(ctx, c.DatabaseURL)
if err != nil {
return nil, err
}
if pending {
return nil, fmt.Errorf("database schema is behind this release, run \"api migrate up\" first")
}
d := &Deps{Log: log}
if d.DB, err = OpenPostgres(ctx, c.DatabaseURL); err != nil {
return nil, err
}
if d.Redis, err = OpenRedis(ctx, c); err != nil {
d.Close()
return nil, err
}
warnIfRLSBypassed(ctx, d)
return d, nil
}
// warnIfRLSBypassed flags deployments where tenant isolation silently degrades
// to application-level filtering only.
func warnIfRLSBypassed(ctx context.Context, d *Deps) {
var bypass bool
err := d.DB.QueryRow(ctx, `SELECT rolsuper OR rolbypassrls FROM pg_roles WHERE rolname = current_user`).Scan(&bypass)
if err == nil && bypass {
d.Log.Warn("database role bypasses row-level security; connect as a non-superuser role without BYPASSRLS")
}
}