github.com/lusis/distribution@v2.0.1+incompatible/health/checks/checks.go (about)

     1  package checks
     2  
     3  import (
     4  	"errors"
     5  	"github.com/docker/distribution/health"
     6  	"net/http"
     7  	"os"
     8  )
     9  
    10  // FileChecker checks the existence of a file and returns and error
    11  // if the file exists, taking the application out of rotation
    12  func FileChecker(f string) health.Checker {
    13  	return health.CheckFunc(func() error {
    14  		if _, err := os.Stat(f); err == nil {
    15  			return errors.New("file exists")
    16  		}
    17  		return nil
    18  	})
    19  }
    20  
    21  // HTTPChecker does a HEAD request and verifies if the HTTP status
    22  // code return is a 200, taking the application out of rotation if
    23  // otherwise
    24  func HTTPChecker(r string) health.Checker {
    25  	return health.CheckFunc(func() error {
    26  		response, err := http.Head(r)
    27  		if err != nil {
    28  			return errors.New("error while checking: " + r)
    29  		}
    30  		if response.StatusCode != http.StatusOK {
    31  			return errors.New("downstream service returned unexpected status: " + string(response.StatusCode))
    32  		}
    33  		return nil
    34  	})
    35  }