github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/health_check.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/gin-gonic/gin"
    24  
    25  	"github.com/mysteriumnetwork/node/metadata"
    26  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    27  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    28  )
    29  
    30  type healthCheckEndpoint struct {
    31  	startTime       time.Time
    32  	currentTimeFunc func() time.Time
    33  	processNumber   int
    34  }
    35  
    36  /*
    37  HealthCheckEndpointFactory creates a structure with single HealthCheck method for healthcheck serving as http,
    38  currentTimeFunc is injected for easier testing
    39  */
    40  func HealthCheckEndpointFactory(currentTimeFunc func() time.Time, procID func() int) *healthCheckEndpoint {
    41  	startTime := currentTimeFunc()
    42  	return &healthCheckEndpoint{
    43  		startTime,
    44  		currentTimeFunc,
    45  		procID(),
    46  	}
    47  }
    48  
    49  // swagger:operation GET /healthcheck Client healthCheck
    50  //
    51  //	---
    52  //	summary: Returns information about client
    53  //	description: Returns health check information about client
    54  //	responses:
    55  //	  200:
    56  //	    description: Health check information
    57  //	    schema:
    58  //	      "$ref": "#/definitions/HealthCheckDTO"
    59  func (hce *healthCheckEndpoint) HealthCheck(c *gin.Context) {
    60  	status := contract.HealthCheckDTO{
    61  		Uptime:  hce.currentTimeFunc().Sub(hce.startTime).String(),
    62  		Process: hce.processNumber,
    63  		Version: metadata.VersionAsString(),
    64  		BuildInfo: contract.BuildInfoDTO{
    65  			Commit:      metadata.BuildCommit,
    66  			Branch:      metadata.BuildBranch,
    67  			BuildNumber: metadata.BuildNumber,
    68  		},
    69  	}
    70  	utils.WriteAsJSON(status, c.Writer)
    71  }