Skip to content

Commit

Permalink
migrate healthcheck to go
Browse files Browse the repository at this point in the history
closes #72
  • Loading branch information
BeryJu committed Jan 1, 2024
1 parent 55bf982 commit 44f0f17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
38 changes: 38 additions & 0 deletions cmd/healthcheck.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 44f0f17

Please sign in to comment.