MCPcopy
hub / github.com/tinyauthapp/tinyauth / healthcheckCmd

Function healthcheckCmd

cmd/tinyauth/healthcheck.go:21–94  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19}
20
21func healthcheckCmd() *cli.Command {
22 return &cli.Command{
23 Name: "healthcheck",
24 Description: "Perform a health check",
25 Configuration: nil,
26 Resources: nil,
27 AllowArg: true,
28 Run: func(args []string) error {
29 tlog.NewSimpleLogger().Init()
30
31 srvAddr := os.Getenv("TINYAUTH_SERVER_ADDRESS")
32 if srvAddr == "" {
33 srvAddr = "127.0.0.1"
34 }
35
36 srvPort := os.Getenv("TINYAUTH_SERVER_PORT")
37 if srvPort == "" {
38 srvPort = "3000"
39 }
40
41 appUrl := fmt.Sprintf("http://%s:%s", srvAddr, srvPort)
42
43 if len(args) > 0 {
44 appUrl = args[0]
45 }
46
47 if appUrl == "" {
48 return errors.New("Could not determine app URL")
49 }
50
51 tlog.App.Info().Str("app_url", appUrl).Msg("Performing health check")
52
53 client := http.Client{
54 Timeout: 30 * time.Second,
55 }
56
57 req, err := http.NewRequest("GET", appUrl+"/api/healthz", nil)
58
59 if err != nil {
60 return fmt.Errorf("failed to create request: %w", err)
61 }
62
63 resp, err := client.Do(req)
64
65 if err != nil {
66 return fmt.Errorf("failed to perform request: %w", err)
67 }
68
69 if resp.StatusCode != http.StatusOK {
70 return fmt.Errorf("service is not healthy, got: %s", resp.Status)
71 }
72
73 defer resp.Body.Close()
74
75 var healthResp healthzResponse
76
77 body, err := io.ReadAll(resp.Body)
78

Callers 1

mainFunction · 0.85

Calls 2

NewSimpleLoggerFunction · 0.92
InitMethod · 0.45

Tested by

no test coverage detected