diff --git a/pluginmgr/webapi/plugins.go b/pluginmgr/webapi/plugins.go index 48b6ba7002cc83baf1d553bbd94bc4967ad0f645..11df33b279241384ec2669b08f688b43ff2ea6f1 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 0000000000000000000000000000000000000000..29af56fdd889537a576ec07e3b77f9d011b6bfd6 --- /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 +}