go-healthchecks/check.go

96 lines
1.5 KiB
Go

package gohealthchecks
import (
"fmt"
"path"
"github.com/google/uuid"
)
type Check interface {
path() string
params() map[string]string
}
type uuidCheck struct {
checkID uuid.UUID
runID uuid.UUID
}
func (c uuidCheck) path() string {
return c.checkID.String()
}
func (c uuidCheck) params() map[string]string {
if c.runID == uuid.Nil {
return nil
}
return map[string]string{
"rid": c.runID.String(),
}
}
func NewUUIDCheck(checkRawID, runRawID string) (Check, error) {
checkID, err := uuid.Parse(checkRawID)
if err != nil {
return nil, err
}
runID := uuid.Nil
if runRawID != "" {
runID, err = uuid.Parse(runRawID)
if err != nil {
return nil, err
}
}
return uuidCheck{
checkID: checkID,
runID: runID,
}, nil
}
type slugCheck struct {
pingKey string
slug string
autoCreate bool
runID uuid.UUID
}
func (c slugCheck) path() string {
return path.Join(c.pingKey, c.slug)
}
func (c slugCheck) params() map[string]string {
res := map[string]string{}
createVal := "0"
if c.autoCreate {
createVal = "1"
}
res["create"] = createVal
if c.runID != uuid.Nil {
res["rid"] = c.runID.String()
}
return res
}
func NewSlugCheck(pingKey, slug string, autoCreate bool, runRawID string) (Check, error) {
runID := uuid.Nil
if runRawID != "" {
var err error
runID, err = uuid.Parse(runRawID)
if err != nil {
return nil, fmt.Errorf("invalid run ID: %w", err)
}
}
return slugCheck{
pingKey: pingKey,
slug: slug,
autoCreate: autoCreate,
runID: runID,
}, nil
}