diff --git a/api/v1/streaming/health/errors.go b/api/v1/streaming/health/errors.go new file mode 100644 index 0000000..8623eca --- /dev/null +++ b/api/v1/streaming/health/errors.go @@ -0,0 +1,9 @@ +package health + +import ( + "github.com/reiver/go-erorr" +) + +const ( + errNilHTTPResponse = erorr.Error("mstdn: nil http-response") +) diff --git a/api/v1/streaming/health/isok.go b/api/v1/streaming/health/isok.go new file mode 100644 index 0000000..570e01a --- /dev/null +++ b/api/v1/streaming/health/isok.go @@ -0,0 +1,36 @@ +package health + +import ( + "net/http" + "net/url" +) + +// IsOK return true if the host is "alive". +// +// The way to checks if the host is "alive" is by making +// an HTTP request to "/api/v1/streaming/health". +// If the HTTP-status-code of the HTTP-response is "200", +// it returns true. +// +// This is part of the Mastodon client-server API. +// +// See: +// https://docs.joinmastodon.org/methods/streaming/#health +func IsOK(host string) (bool, error) { + var urloc = url.URL{ + Scheme:"https", + Host:host, + Path:Path, + } + + resp, err := http.Get(urloc.String()) + if nil != err { + return false, err + } + + if nil == resp { + return false, errNilHTTPResponse + } + + return http.StatusOK == resp.StatusCode, nil +} diff --git a/api/v1/streaming/health/path.go b/api/v1/streaming/health/path.go new file mode 100644 index 0000000..ccc03db --- /dev/null +++ b/api/v1/streaming/health/path.go @@ -0,0 +1,5 @@ +package health + +const Path string = "/api/v1/streaming/health" + +