Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure timeout for the health check. #4

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions cmd/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package main

import (
"context"
"flag"
"net/http"
"os"
"time"
)

func main() {
url := flag.String("url", "", "URL to check")
filePath := flag.String("file", "", "File path to check")
var flagURL, flagFilePath string
var flagTimeout time.Duration
flag.StringVar(&flagURL, "url", "", "URL to check")
flag.StringVar(&flagFilePath, "file", "", "File path to check")
flag.DurationVar(&flagTimeout, "timeout", time.Second*5, "Timeout for the request")

flag.Parse()

if *url != "" {
resp, err := http.Get(*url)
if flagURL != "" {
ctx, cancel := context.WithTimeout(context.Background(), flagTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, flagURL, nil)
if err != nil {
os.Exit(1)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
os.Exit(1)
}
Expand All @@ -23,8 +35,8 @@ func main() {
}
}

if *filePath != "" {
if _, err := os.Stat(*filePath); os.IsNotExist(err) {
if flagFilePath != "" {
if _, err := os.Stat(flagFilePath); os.IsNotExist(err) {
os.Exit(1)
}
}
Expand Down
Loading