From 954b4bb6fefd374ab68e5419bb8e521697732e8f Mon Sep 17 00:00:00 2001
From: Acha Bill <57879913+acha-bill@users.noreply.github.com>
Date: Tue, 9 Jun 2020 15:28:53 +0100
Subject: [PATCH] add healthz endpoint (#455)

* add healthz endpoint

* fix comment
---
 pluginmgr/webapi/plugins.go      |  2 ++
 plugins/webapi/healthz/plugin.go | 44 ++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 plugins/webapi/healthz/plugin.go

diff --git a/pluginmgr/webapi/plugins.go b/pluginmgr/webapi/plugins.go
index 48b6ba70..11df33b2 100644
--- a/pluginmgr/webapi/plugins.go
+++ b/pluginmgr/webapi/plugins.go
@@ -5,6 +5,7 @@ import (
 	"github.com/iotaledger/goshimmer/plugins/webapi/autopeering"
 	"github.com/iotaledger/goshimmer/plugins/webapi/data"
 	"github.com/iotaledger/goshimmer/plugins/webapi/drng"
+	"github.com/iotaledger/goshimmer/plugins/webapi/healthz"
 	"github.com/iotaledger/goshimmer/plugins/webapi/info"
 	"github.com/iotaledger/goshimmer/plugins/webapi/message"
 	"github.com/iotaledger/goshimmer/plugins/webapi/spammer"
@@ -19,6 +20,7 @@ var PLUGINS = node.Plugins(
 	spammer.Plugin,
 	data.Plugin,
 	drng.Plugin,
+	healthz.Plugin,
 	message.Plugin,
 	autopeering.Plugin,
 	info.Plugin,
diff --git a/plugins/webapi/healthz/plugin.go b/plugins/webapi/healthz/plugin.go
new file mode 100644
index 00000000..29af56fd
--- /dev/null
+++ b/plugins/webapi/healthz/plugin.go
@@ -0,0 +1,44 @@
+package healthz
+
+import (
+	"net/http"
+
+	"github.com/iotaledger/goshimmer/plugins/gossip"
+	"github.com/iotaledger/goshimmer/plugins/sync"
+	"github.com/iotaledger/goshimmer/plugins/webapi"
+	"github.com/iotaledger/hive.go/node"
+	"github.com/labstack/echo"
+)
+
+// PluginName is the name of the web API healthz endpoint plugin.
+const PluginName = "WebAPI healthz Endpoint"
+
+// Plugin is the plugin instance of the web API info endpoint plugin.
+var Plugin = node.NewPlugin(PluginName, node.Enabled, configure)
+
+func configure(_ *node.Plugin) {
+	webapi.Server.GET("healthz", getHealthz)
+}
+
+func getHealthz(c echo.Context) error {
+	if !IsNodeHealthy() {
+		return c.NoContent(http.StatusServiceUnavailable)
+	}
+
+	return c.NoContent(http.StatusOK)
+}
+
+// IsNodeHealthy returns whether the node is synced, has active neighbors.
+func IsNodeHealthy() bool {
+	// Synced
+	if !sync.Synced() {
+		return false
+	}
+
+	// Has connected neighbors
+	if len(gossip.Manager().AllNeighbors()) == 0 {
+		return false
+	}
+
+	return true
+}
-- 
GitLab