mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
refactor: rework cli healthcheck logic
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"tinyauth/internal/config"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -23,7 +22,6 @@ type healthCmd struct {
|
|||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
|
|
||||||
viper *viper.Viper
|
viper *viper.Viper
|
||||||
appUrl string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHealthCmd(root *cobra.Command) *healthCmd {
|
func newHealthCmd(root *cobra.Command) *healthCmd {
|
||||||
@@ -42,9 +40,6 @@ func (c *healthCmd) Register() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.viper.AutomaticEnv()
|
c.viper.AutomaticEnv()
|
||||||
c.cmd.Flags().StringVar(&c.appUrl, "app-url", "http://localhost:3000", "The URL where the Tinyauth server is running on.")
|
|
||||||
c.viper.BindEnv("app-url", "APP_URL")
|
|
||||||
c.viper.BindPFlags(c.cmd.Flags())
|
|
||||||
|
|
||||||
if c.root != nil {
|
if c.root != nil {
|
||||||
c.root.AddCommand(c.cmd)
|
c.root.AddCommand(c.cmd)
|
||||||
@@ -58,19 +53,19 @@ func (c *healthCmd) GetCmd() *cobra.Command {
|
|||||||
func (c *healthCmd) run(cmd *cobra.Command, args []string) {
|
func (c *healthCmd) run(cmd *cobra.Command, args []string) {
|
||||||
log.Logger = log.Level(zerolog.InfoLevel)
|
log.Logger = log.Level(zerolog.InfoLevel)
|
||||||
|
|
||||||
appUrl := c.viper.GetString("app-url")
|
appUrl := "http://127.0.0.1:3000"
|
||||||
|
|
||||||
if appUrl == "" {
|
port := c.viper.GetString("PORT")
|
||||||
log.Fatal().Err(errors.New("app-url is required")).Msg("App URL is required")
|
address := c.viper.GetString("ADDRESS")
|
||||||
|
|
||||||
|
if address != "" && port != "" {
|
||||||
|
appUrl = "http://" + address + ":" + port
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Version == "development" {
|
if len(args) > 0 {
|
||||||
log.Warn().Msg("Running in development mode. Overriding the app-url to http://localhost:3000")
|
appUrl = args[0]
|
||||||
appUrl = "http://localhost:3000"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msgf("Health check endpoint is available at %s/api/healthz", appUrl)
|
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", appUrl+"/api/healthz", nil)
|
req, err := http.NewRequest("GET", appUrl+"/api/healthz", nil)
|
||||||
@@ -105,5 +100,5 @@ func (c *healthCmd) run(cmd *cobra.Command, args []string) {
|
|||||||
log.Fatal().Err(err).Msg("Failed to decode response")
|
log.Fatal().Err(err).Msg("Failed to decode response")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Interface("response", healthResp).Msg("Service is healthy")
|
log.Info().Interface("response", healthResp).Msg("Tinyauth is healthy")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user