github.com/msales/pkg/v3@v3.24.0/health/server.go (about)

     1  package health
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  // DefaultPattern is the default health http path.
    10  var DefaultPattern = "/health"
    11  
    12  // DefaultHandler is the default health http handler.
    13  var DefaultHandler = NewHandler()
    14  
    15  var server = &http.Server{
    16  	ReadTimeout:  5 * time.Second,
    17  	WriteTimeout: 5 * time.Second,
    18  }
    19  
    20  // StartServer starts the http health server on the given port
    21  // with the given reporters.
    22  func StartServer(addr string, reporters ...Reporter) error {
    23  	server.Addr = addr
    24  	server.Handler = newMux(reporters)
    25  
    26  	return server.ListenAndServe()
    27  }
    28  
    29  // StopServer stops the htt[ health server
    30  func StopServer() error {
    31  	return server.Shutdown(context.Background())
    32  }
    33  
    34  func newMux(reporters []Reporter) http.Handler {
    35  	mux := &http.ServeMux{}
    36  	mux.Handle(DefaultPattern, DefaultHandler.With(reporters...))
    37  
    38  	return mux
    39  }