Request access logs, panics, migration output and fatal errors are now emitted through the JSON slog handler. BREAKING CHANGE: UPTIME_LOG_FORMAT is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
65 lines
1.4 KiB
Go
65 lines
1.4 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"
|
|
)
|
|
|
|
type Deps struct {
|
|
Log *slog.Logger
|
|
DB *pgxpool.Pool
|
|
Redis *redis.Client
|
|
}
|
|
|
|
func (d *Deps) Close() {
|
|
if d.DB != nil {
|
|
d.DB.Close()
|
|
}
|
|
if d.Redis != nil {
|
|
_ = d.Redis.Close()
|
|
}
|
|
}
|
|
|
|
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() },
|
|
}
|
|
}
|
|
|
|
func SignalContext() (context.Context, context.CancelFunc) {
|
|
return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
}
|
|
|
|
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
|
|
}
|
|
return d, nil
|
|
}
|