From a77c8ac8dd98647e4428c7c3ecffb363b022b072 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 17 Dec 2021 15:14:01 +0100 Subject: [PATCH] add version to accounts --- accounts/pkg/command/health.go | 51 ++++++++++++++++++++++++++++++++++ accounts/pkg/command/root.go | 4 ++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 accounts/pkg/command/health.go diff --git a/accounts/pkg/command/health.go b/accounts/pkg/command/health.go new file mode 100644 index 0000000000..5a4d79cef2 --- /dev/null +++ b/accounts/pkg/command/health.go @@ -0,0 +1,51 @@ +package command + +import ( + "fmt" + "net/http" + + "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/pkg/logging" + "github.com/urfave/cli/v2" +) + +// Health is the entrypoint for the health command. +func Health(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "health", + Usage: "Check health status", + Before: func(c *cli.Context) error { + return ParseConfig(c, cfg) + }, + Action: func(c *cli.Context) error { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + + resp, err := http.Get( + fmt.Sprintf( + "http://%s/healthz", + cfg.Debug.Addr, + ), + ) + + if err != nil { + logger.Fatal(). + Err(err). + Msg("Failed to request health check") + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + logger.Fatal(). + Int("code", resp.StatusCode). + Msg("Health seems to be in bad state") + } + + logger.Debug(). + Int("code", resp.StatusCode). + Msg("Health got a good state") + + return nil + }, + } +} diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 97088ea205..5854e0f601 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -39,8 +39,10 @@ func Execute(cfg *config.Config) error { ListAccounts(cfg), InspectAccount(cfg), RemoveAccount(cfg), - PrintVersion(cfg), RebuildIndex(cfg), + + Health(cfg), + PrintVersion(cfg), }, }