...

Source file src/edge-infra.dev/pkg/edge/datasync/chirp/server/http/liveness.go

Documentation: edge-infra.dev/pkg/edge/datasync/chirp/server/http

     1  package http
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"edge-infra.dev/pkg/lib/fog"
     8  
     9  	dsHTTP "edge-infra.dev/pkg/edge/datasync/http"
    10  	"edge-infra.dev/pkg/edge/datasync/internal/config"
    11  )
    12  
    13  var logger = fog.New()
    14  
    15  func NewLivenessServer(shouldRun chan bool, cfg *config.Config) *dsHTTP.Server {
    16  	serverMux := http.NewServeMux()
    17  	serverMux.HandleFunc("/healthz", healthz)
    18  
    19  	return dsHTTP.NewServer(cfg.LivenessPort, serverMux, shouldRun)
    20  }
    21  
    22  func healthz(w http.ResponseWriter, _ *http.Request) {
    23  	isAlive := isServerAlive()
    24  	if isAlive {
    25  		fmt.Fprintf(w, "up")
    26  		return
    27  	}
    28  	w.WriteHeader(http.StatusInternalServerError)
    29  	fmt.Fprintf(w, "down")
    30  }
    31  
    32  // isServerAlive performs a check to ensure the server is still in a healthy and useable state
    33  func isServerAlive() bool {
    34  	// TODO: Find a better way to check for "healthyness" than just validating config
    35  	// over and over. Config should only need to be validated once on startup anyways since it doesn't change.
    36  	err := config.ValidateConfiguration()
    37  	if err != nil {
    38  		logger.Error(err, "Liveness check failed")
    39  	}
    40  	return err == nil
    41  }
    42  

View as plain text