Files
coder/coderd/healthcheck/accessurl.go
Kyle Carberry 22e781eced chore: add /v2 to import module path (#9072)
* chore: add /v2 to import module path

go mod requires semantic versioning with versions greater than 1.x

This was a mechanical update by running:
```
go install github.com/marwan-at-work/mod/cmd/mod@latest
mod upgrade
```

Migrate generated files to import /v2

* Fix gen
2023-08-18 18:55:43 +00:00

74 lines
1.7 KiB
Go

package healthcheck
import (
"context"
"io"
"net/http"
"net/url"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/util/ptr"
)
// @typescript-generate AccessURLReport
type AccessURLReport struct {
AccessURL string `json:"access_url"`
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
StatusCode int `json:"status_code"`
HealthzResponse string `json:"healthz_response"`
Error *string `json:"error"`
}
type AccessURLReportOptions struct {
AccessURL *url.URL
Client *http.Client
}
func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if opts.AccessURL == nil {
r.Error = ptr.Ref("access URL is nil")
return
}
r.AccessURL = opts.AccessURL.String()
if opts.Client == nil {
opts.Client = http.DefaultClient
}
accessURL, err := opts.AccessURL.Parse("/healthz")
if err != nil {
r.Error = convertError(xerrors.Errorf("parse healthz endpoint: %w", err))
return
}
req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil)
if err != nil {
r.Error = convertError(xerrors.Errorf("create healthz request: %w", err))
return
}
res, err := opts.Client.Do(req)
if err != nil {
r.Error = convertError(xerrors.Errorf("get healthz endpoint: %w", err))
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
r.Error = convertError(xerrors.Errorf("read healthz response: %w", err))
return
}
r.Reachable = true
r.Healthy = res.StatusCode == http.StatusOK
r.StatusCode = res.StatusCode
r.HealthzResponse = string(body)
}