mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2025-12-28 08:38:12 -05:00
36 lines
687 B
Go
36 lines
687 B
Go
package checks
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/opencloud-eu/opencloud/pkg/handlers"
|
|
)
|
|
|
|
// NewHTTPCheck checks the reachability of a http server.
|
|
func NewHTTPCheck(url string) func(context.Context) error {
|
|
return func(_ context.Context) error {
|
|
url, err := handlers.FailSaveAddress(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
|
url = "http://" + url
|
|
}
|
|
|
|
c := http.Client{
|
|
Timeout: 3 * time.Second,
|
|
}
|
|
resp, err := c.Get(url)
|
|
if err != nil {
|
|
return fmt.Errorf("could not connect to http server: %v", err)
|
|
}
|
|
_ = resp.Body.Close()
|
|
return nil
|
|
}
|
|
}
|