diff --git a/Dockerfile b/Dockerfile index 7f277cf..54436f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,5 +6,5 @@ COPY oidc-test-client / EXPOSE 9009 WORKDIR /web-root ENV OIDC_BIND=0.0.0.0:9009 -HEALTHCHECK --interval=5s --start-period=1s CMD [ "wget", "--spider", "http://localhost:9009/health" ] +HEALTHCHECK --interval=5s --start-period=1s CMD [ "/oidc-test-client", "healthcheck" ] ENTRYPOINT [ "/oidc-test-client" ] diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go new file mode 100644 index 0000000..b7d1804 --- /dev/null +++ b/cmd/healthcheck.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "net/http" + "os" + + "beryju.io/oidc-test-client/pkg" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var healthcheckCmd = &cobra.Command{ + Use: "healthcheck", + Run: func(cmd *cobra.Command, args []string) { + os.Exit(check()) + }, +} + +func check() int { + root := pkg.Env("OIDC_ROOT_URL", "http://localhost:9009") + url := fmt.Sprintf("%s/health", root) + res, err := http.DefaultClient.Head(url) + if err != nil { + log.WithError(err).Warning("failed to send healthcheck request") + return 1 + } + if res.StatusCode >= 400 { + log.WithField("status", res.StatusCode).Warning("unhealthy status code") + return 1 + } + log.Debug("successfully checked health") + return 0 +} + +func init() { + rootCmd.AddCommand(healthcheckCmd) +}