updated deps; updated healthchecks.io integration
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
# healthchecks
|
||||
|
||||
A [healthchecks.io](https://github.com/healthchecks/healthchecks) wrapper
|
||||
A [healthchecks.io](https://github.com/healthchecks/healthchecks) client
|
||||
|
||||
check the godoc for information
|
||||
|
||||
```go
|
||||
hc := healthchecks.New("your-uuid")
|
||||
hc := healthchecks.New(
|
||||
healthchecks.WithCheckUUID("your-uuid"),
|
||||
)
|
||||
go hc.Auto()
|
||||
|
||||
hc.Log(strings.NewReader("optional body you can attach to any action"))
|
||||
@@ -5,26 +5,58 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Client for healthchecks
|
||||
type Client struct {
|
||||
HTTP *http.Client
|
||||
http *http.Client
|
||||
log func(string, error)
|
||||
baseURL string
|
||||
uuid string
|
||||
rid string
|
||||
create bool
|
||||
done chan bool
|
||||
}
|
||||
|
||||
// init client
|
||||
func (c *Client) init(options ...Option) {
|
||||
for _, option := range options {
|
||||
option(c)
|
||||
}
|
||||
if c.log == nil {
|
||||
c.log = DefaultErrLog
|
||||
}
|
||||
if c.baseURL == "" {
|
||||
c.baseURL = DefaultAPI
|
||||
}
|
||||
if c.http == nil {
|
||||
c.http = &http.Client{Timeout: 10 * time.Second}
|
||||
}
|
||||
if c.done == nil {
|
||||
c.done = make(chan bool, 1)
|
||||
}
|
||||
if c.uuid == "" {
|
||||
randomUUID, _ := uuid.NewRandom()
|
||||
c.uuid = randomUUID.String()
|
||||
c.create = true
|
||||
c.log("uuid", fmt.Errorf("check UUID is not provided, using random %q with auto provision", c.uuid))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) call(operation, endpoint string, body ...io.Reader) {
|
||||
var err error
|
||||
var resp *http.Response
|
||||
targetURL := fmt.Sprintf("%s/%s%s?rid=%s", c.baseURL, c.uuid, endpoint, c.rid)
|
||||
if c.create {
|
||||
targetURL += "&create=1"
|
||||
}
|
||||
if len(body) > 0 {
|
||||
resp, err = c.HTTP.Post(targetURL, "text/plain; charset=utf-8", body[0])
|
||||
resp, err = c.http.Post(targetURL, "text/plain; charset=utf-8", body[0])
|
||||
} else {
|
||||
resp, err = c.HTTP.Head(targetURL)
|
||||
resp, err = c.http.Head(targetURL)
|
||||
}
|
||||
if err != nil {
|
||||
c.log(operation, err)
|
||||
@@ -32,7 +64,7 @@ func (c *Client) call(operation, endpoint string, body ...io.Reader) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
respb, rerr := io.ReadAll(resp.Body)
|
||||
if rerr != nil {
|
||||
c.log(operation+":response", rerr)
|
||||
@@ -2,8 +2,6 @@ package healthchecks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -20,21 +18,12 @@ var DefaultErrLog = func(operation string, err error) {
|
||||
}
|
||||
|
||||
// New healthchecks client
|
||||
func New(hcUUID string, errlog ...ErrLog) *Client {
|
||||
func New(options ...Option) *Client {
|
||||
rid, _ := uuid.NewRandom()
|
||||
c := &Client{
|
||||
baseURL: DefaultAPI,
|
||||
uuid: hcUUID,
|
||||
rid: rid.String(),
|
||||
done: make(chan bool, 1),
|
||||
}
|
||||
c.HTTP = &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
c.log = DefaultErrLog
|
||||
if len(errlog) > 0 {
|
||||
c.log = errlog[0]
|
||||
rid: rid.String(),
|
||||
}
|
||||
c.init(options...)
|
||||
|
||||
return c
|
||||
}
|
||||
22
vendor/gitlab.com/etke.cc/go/healthchecks/v2/justfile
generated
vendored
Normal file
22
vendor/gitlab.com/etke.cc/go/healthchecks/v2/justfile
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
# show help by default
|
||||
default:
|
||||
@just --list --justfile {{ justfile() }}
|
||||
|
||||
# update go deps
|
||||
update *flags:
|
||||
go get {{flags}} .
|
||||
go mod tidy
|
||||
|
||||
# run linter
|
||||
lint:
|
||||
golangci-lint run ./...
|
||||
|
||||
# automatically fix liter issues
|
||||
lintfix:
|
||||
golangci-lint run --fix ./...
|
||||
|
||||
# run unit tests
|
||||
test:
|
||||
@go test -cover -coverprofile=cover.out -coverpkg=./... -covermode=set ./...
|
||||
@go tool cover -func=cover.out
|
||||
-@rm -f cover.out
|
||||
47
vendor/gitlab.com/etke.cc/go/healthchecks/v2/options.go
generated
vendored
Normal file
47
vendor/gitlab.com/etke.cc/go/healthchecks/v2/options.go
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
package healthchecks
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Option func(*Client)
|
||||
|
||||
// WithHTTPClient sets the http client
|
||||
func WithHTTPClient(httpClient *http.Client) Option {
|
||||
return func(c *Client) {
|
||||
c.http = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// WithBaseURL sets the base url
|
||||
func WithBaseURL(baseURL string) Option {
|
||||
return func(c *Client) {
|
||||
c.baseURL = baseURL
|
||||
}
|
||||
}
|
||||
|
||||
// WithErrLog sets the error log
|
||||
func WithErrLog(errLog ErrLog) Option {
|
||||
return func(c *Client) {
|
||||
c.log = errLog
|
||||
}
|
||||
}
|
||||
|
||||
// WithCheckUUID sets the check UUID
|
||||
func WithCheckUUID(uuid string) Option {
|
||||
return func(c *Client) {
|
||||
c.uuid = uuid
|
||||
}
|
||||
}
|
||||
|
||||
// WithAutoProvision enables auto provision
|
||||
func WithAutoProvision() Option {
|
||||
return func(c *Client) {
|
||||
c.create = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithDone sets the done channel
|
||||
func WithDone(done chan bool) Option {
|
||||
return func(c *Client) {
|
||||
c.done = done
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user