github.com/uvalib/orcid-access-ws@v0.0.0-20250612130209-7d062dbabf9d/orcidaccessws/handlers/healthcheck_handler.go (about)

     1  package handlers
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/uvalib/orcid-access-ws/orcidaccessws/dao"
     6  	"github.com/uvalib/orcid-access-ws/orcidaccessws/logger"
     7  	"github.com/uvalib/orcid-access-ws/orcidaccessws/orcid"
     8  	"net/http"
     9  )
    10  
    11  // HealthCheck -- do the healthcheck
    12  func HealthCheck(w http.ResponseWriter, r *http.Request) {
    13  
    14  	status := http.StatusOK
    15  	dbErr := dao.Store.Check()
    16  	orcidPublicErr := orcid.GetPublicEndpointStatus()
    17  	orcidSecureErr := orcid.GetSecureEndpointStatus()
    18  
    19  	var dbMessage, orcidPublicMessage, orcidSecureMessage string
    20  
    21  	if dbErr != nil || orcidPublicErr != nil || orcidSecureErr != nil {
    22  
    23  		if dbErr != nil {
    24  			// only a database connection problem is considered an error (cos we can actually do something
    25  			// about it)...
    26  			status = http.StatusInternalServerError
    27  
    28  			dbMessage = dbErr.Error()
    29  			logger.Log(fmt.Sprintf("ERROR: Datastore reports '%s'", dbMessage))
    30  		}
    31  
    32  		if orcidPublicErr != nil {
    33  			orcidPublicMessage = orcidPublicErr.Error()
    34  			logger.Log(fmt.Sprintf("ERROR: ORCID public endpoint reports '%s'", orcidPublicMessage))
    35  		}
    36  
    37  		if orcidSecureErr != nil {
    38  			orcidSecureMessage = orcidSecureErr.Error()
    39  			logger.Log(fmt.Sprintf("ERROR: ORCID secure endpoint reports '%s'", orcidSecureMessage))
    40  		}
    41  	}
    42  
    43  	encodeHealthCheckResponse(w, status, dbMessage, orcidPublicMessage, orcidSecureMessage)
    44  }
    45  
    46  //
    47  // end of file
    48  //