From eca6289ce55e7ea43eea18121c0a84e12d089f18 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 1 Aug 2024 18:27:14 -0700 Subject: [PATCH] /api/v1/streaming/health --- api/v1/streaming/health/errors.go | 9 ++++++++ api/v1/streaming/health/isok.go | 36 +++++++++++++++++++++++++++++++ api/v1/streaming/health/path.go | 5 +++++ 3 files changed, 50 insertions(+) create mode 100644 api/v1/streaming/health/errors.go create mode 100644 api/v1/streaming/health/isok.go create mode 100644 api/v1/streaming/health/path.go 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" + +