Skip to content

Commit

Permalink
🚑 header checker
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultleouay committed Apr 21, 2024
1 parent 55ad383 commit ae79555
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions apps/checker/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ type Response struct {

func Ping(ctx context.Context, client *http.Client, inputData request.CheckerRequest) (PingData, error) {
logger := log.Ctx(ctx).With().Str("monitor", inputData.URL).Logger()

region := os.Getenv("FLY_REGION")
req, err := http.NewRequestWithContext(ctx, inputData.Method, inputData.URL, bytes.NewReader([]byte(inputData.Body)))
if err != nil {
logger.Error().Err(err).Msg("error while creating req")
return PingData{}, fmt.Errorf("unable to create req: %w", err)
}

req.Header.Set("User-Agent", "OpenStatus/1.0")
for _, header := range inputData.Headers {
if header.Key != "" {
req.Header.Set(header.Key, header.Value)
}
}
if inputData.Method != http.MethodGet {
req.Header.Set("Content-Type", "application/json")
}

timing := Timing{}

Expand All @@ -99,9 +100,9 @@ func Ping(ctx context.Context, client *http.Client, inputData request.CheckerReq
start := time.Now()

response, err := client.Do(req)
fmt.Println(response.StatusCode)
timing.TransferDone = time.Now().UTC().UnixMilli()
latency := time.Since(start).Milliseconds()

if err != nil {
timingAsString, err2 := json.Marshal(timing)
if err2 != nil {
Expand All @@ -128,6 +129,7 @@ func Ping(ctx context.Context, client *http.Client, inputData request.CheckerReq
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
fmt.Println(string(body))
if err != nil {
return PingData{
Latency: latency,
Expand Down

0 comments on commit ae79555

Please sign in to comment.