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
This commit is contained in:
1 parent
048e114fd9
commit
d20225982a
21 files changed
+147
-36
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Probe reports whether a dependency is usable.
|
||||
type Probe func(context.Context) error
|
||||
|
||||
// Health separates liveness from readiness so a draining instance drops out
|
||||
@@ -18,12 +19,15 @@ type Health struct {
|
||||
draining atomic.Bool
|
||||
}
|
||||
|
||||
// NewHealth returns a Health that is ready while all probes pass.
|
||||
func NewHealth(probes map[string]Probe) *Health {
|
||||
return &Health{probes: probes}
|
||||
}
|
||||
|
||||
// Drain makes /readyz fail permanently. Call it before shutdown.
|
||||
func (h *Health) Drain() { h.draining.Store(true) }
|
||||
|
||||
// Register mounts /healthz (liveness) and /readyz (readiness).
|
||||
func (h *Health) Register(r gin.IRoutes) {
|
||||
r.GET("/healthz", func(c *gin.Context) { c.Status(http.StatusNoContent) })
|
||||
r.GET("/readyz", h.ready)
|
||||
|
||||
@@ -7,15 +7,13 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo"
|
||||
)
|
||||
|
||||
var logLevel = new(slog.LevelVar)
|
||||
|
||||
// NewLogger installs a JSON logger as the process default so output from the
|
||||
// standard log package and dependencies ends up in the same format.
|
||||
func NewLogger(component string) *slog.Logger {
|
||||
l := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})).
|
||||
// NewLogger returns a JSON logger tagged with component and the release
|
||||
// version, and the level variable controlling it. The level can be changed
|
||||
// once configuration is loaded; the logger is needed before that to report
|
||||
// configuration errors.
|
||||
func NewLogger(component string) (*slog.Logger, *slog.LevelVar) {
|
||||
level := new(slog.LevelVar)
|
||||
l := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: level})).
|
||||
With("component", component, "app_version", buildinfo.Version)
|
||||
slog.SetDefault(l)
|
||||
return l
|
||||
return l, level
|
||||
}
|
||||
|
||||
func SetLogLevel(l slog.Level) { logLevel.Set(l) }
|
||||
@@ -1,3 +1,5 @@
|
||||
// Package platform wires the infrastructure shared by all commands:
|
||||
// logging, Postgres, Redis, HTTP serving and health probes.
|
||||
package platform
|
||||
|
||||
import (
|
||||
@@ -7,6 +9,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// OpenPostgres connects a pool and verifies it with a ping.
|
||||
func OpenPostgres(ctx context.Context, url string) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
)
|
||||
|
||||
// OpenRedis connects to a single server or, when c.RedisSentinel is enabled,
|
||||
// to the current master via sentinel with automatic failover.
|
||||
func OpenRedis(ctx context.Context, c config.Common) (*redis.Client, error) {
|
||||
var rdb *redis.Client
|
||||
if c.RedisSentinel.Enabled() {
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
|
||||
var quietPaths = map[string]bool{"/healthz": true, "/readyz": true}
|
||||
|
||||
// NewRouter returns a gin engine that logs requests and panics through log
|
||||
// as JSON and writes nothing to stdout itself.
|
||||
func NewRouter(log *slog.Logger) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
gin.DefaultWriter = io.Discard
|
||||
|
||||
@@ -15,12 +15,14 @@ import (
|
||||
"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()
|
||||
@@ -30,6 +32,7 @@ func (d *Deps) 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) },
|
||||
@@ -37,10 +40,13 @@ func (d *Deps) Probes() map[string]Probe {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
@@ -34,6 +34,7 @@ func Serve(ctx context.Context, log *slog.Logger, srv *http.Server, health *Heal
|
||||
return srv.Shutdown(sctx)
|
||||
}
|
||||
|
||||
// NewHTTPServer returns a server with timeouts that bound slow clients.
|
||||
func NewHTTPServer(addr string, h http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
Reference in new issue
Block a user