go-healthchecks/check.go

97 lines
1.5 KiB
Go
Raw Normal View History

2024-10-02 13:26:14 +00:00
package gohealthchecks
import (
2024-10-02 14:11:24 +00:00
"fmt"
2024-10-02 13:26:14 +00:00
"path"
"github.com/google/uuid"
)
type Check interface {
path() string
params() map[string]string
}
2024-10-02 14:11:24 +00:00
type uuidCheck struct {
checkID uuid.UUID
runID uuid.UUID
}
2024-10-02 13:26:14 +00:00
func (c uuidCheck) path() string {
2024-10-02 14:11:24 +00:00
return c.checkID.String()
2024-10-02 13:26:14 +00:00
}
func (c uuidCheck) params() map[string]string {
2024-10-02 14:11:24 +00:00
if c.runID == uuid.Nil {
return nil
}
return map[string]string{
"rid": c.runID.String(),
}
2024-10-02 13:26:14 +00:00
}
2024-10-02 14:11:24 +00:00
func NewUUIDCheck(checkRawID, runRawID string) (Check, error) {
checkID, err := uuid.Parse(checkRawID)
2024-10-02 13:26:14 +00:00
if err != nil {
return nil, err
}
2024-10-02 14:11:24 +00:00
runID := uuid.Nil
if runRawID != "" {
runID, err = uuid.Parse(runRawID)
if err != nil {
return nil, err
}
}
return uuidCheck{
checkID: checkID,
runID: runID,
}, nil
2024-10-02 13:26:14 +00:00
}
type slugCheck struct {
pingKey string
slug string
autoCreate bool
2024-10-02 14:11:24 +00:00
runID uuid.UUID
2024-10-02 13:26:14 +00:00
}
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
2024-10-02 14:11:24 +00:00
if c.runID != uuid.Nil {
res["rid"] = c.runID.String()
}
2024-10-02 13:26:14 +00:00
return res
}
2024-10-02 14:11:24 +00:00
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)
}
}
2024-10-02 13:26:14 +00:00
return slugCheck{
pingKey: pingKey,
slug: slug,
autoCreate: autoCreate,
2024-10-02 14:11:24 +00:00
runID: runID,
}, nil
2024-10-02 13:26:14 +00:00
}