github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/health/server.go (about)

     1  package health
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/gorilla/mux"
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  type Server struct {
    12  	Address string
    13  	Log     log.FieldLogger
    14  }
    15  
    16  func NewServer(host, port string, log *log.Logger) *Server {
    17  	return &Server{
    18  		Address: fmt.Sprintf("%s:%s", host, port),
    19  		Log:     log.WithField("server", "health"),
    20  	}
    21  }
    22  
    23  func (srv *Server) ServeAsync() {
    24  	healthRouter := mux.NewRouter()
    25  	healthRouter.HandleFunc("/healthz", livenessHandler())
    26  	go func() {
    27  		err := http.ListenAndServe(srv.Address, healthRouter)
    28  		if err != nil {
    29  			srv.Log.Errorf("HTTP Health server ListenAndServe: %v", err)
    30  		}
    31  	}()
    32  }
    33  
    34  func livenessHandler() func(w http.ResponseWriter, _ *http.Request) {
    35  	return func(w http.ResponseWriter, _ *http.Request) {
    36  		w.WriteHeader(http.StatusOK)
    37  		return
    38  	}
    39  }