github.com/braveheart12/insolar-09-08-19@v0.8.7/api/healthcheck.go (about)

     1  //
     2  // Copyright 2019 Insolar Technologies GmbH
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package api
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"github.com/insolar/insolar/core"
    23  )
    24  
    25  // HealthChecker allows to check network status of a node.
    26  type HealthChecker struct {
    27  	CertificateManager core.CertificateManager
    28  	NodeNetwork        core.NodeNetwork
    29  }
    30  
    31  // NewHealthChecker creates new HealthChecker.
    32  func NewHealthChecker(cm core.CertificateManager, nn core.NodeNetwork) *HealthChecker {
    33  	return &HealthChecker{CertificateManager: cm, NodeNetwork: nn}
    34  }
    35  
    36  // CheckHandler is a HTTP handler for health check.
    37  func (hc *HealthChecker) CheckHandler(w http.ResponseWriter, r *http.Request) {
    38  	w.Header().Set("Content-Type", "text/plain")
    39  	for _, node := range hc.CertificateManager.GetCertificate().GetDiscoveryNodes() {
    40  		if hc.NodeNetwork.GetWorkingNode(*node.GetNodeRef()) == nil {
    41  			w.WriteHeader(http.StatusInternalServerError)
    42  			_, _ = w.Write([]byte("FAIL"))
    43  			return
    44  		}
    45  	}
    46  	w.WriteHeader(http.StatusOK)
    47  	_, _ = w.Write([]byte("OK"))
    48  }