Skip to content

gritzkoo/golang-health-checker-lw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Health Checker lightweight

test build Coverage Status views views per week clones clones per week CodeQL GitHub Go Reference GitHub go.mod Go version GitHub repo size Go Report Card GitHub issues GitHub Release Date


Welcome!

This package is a younger brother of the Golang Health Checker package that includes only a way to standardize checks of any kind of application and its integrations.

The main purpose of this repository is to disponibility a more simple and lightweight package, with no dependencies, avoiding security issues and extra install packages.

If you are already familiar with Golang Health Checker, this package has the same way of working, you need to create a list of tests to create an endpoint in your API to test your integrations, and get an interface with all status for each integration you passes to this package.


How can I install this package?

go get github.com/gritzkoo/golang-health-checker-lw

How can I use this package?

You can actually test any kind of thing, you just need to declare a function in your application that returns a healthchecker.CheckResponse struct and DONE!

Below is an example of HOW TO create a Handle function to inject into this package.

package main

import (
  "encoding/json"
  "fmt"
  "net/http"

  "github.com/gritzkoo/golang-health-checker-lw/pkg/healthchecker"
)
// declaring a function that will test somenting
// this example test a http request calling
// https://github.com/status to check if GitHub is up and running
func TestMyApi() healthchecker.CheckResponse {
  result := healthchecker.CheckResponse{
    URL: "http://github.com/status",
  }
  client := http.Client{}
  request, err := http.NewRequest("GET", result.URL, nil)
  if err != nil {
    result.Error = err
    return result
  }
  response, err := client.Do(request)
  if err != nil {
    result.Error = err
    return result
  }
  if response.StatusCode != http.StatusOK {
    result.Error = fmt.Errorf("The API returned a status code different of 200! code: %d", response.StatusCode)
  }
  return result
}
// create a pointer with for your application of HealthChecker actor
// to be used in your http interface
var checker = healthchecker.New(healthchecker.Config{
  Name:    "My app name", // optional parameter, should be your application name
  Version: "v1.0.0", // or get it from some where and replace it here!
  Integrations: []healthchecker.Check{ // the list of things you need to check
    {
      Name:   "Github integration",
      Handle: TestMyApi,
    },
  },
})

func main() {
  // declaring a endpoint to liveness
  http.HandleFunc("/health-check/liveness", func(w http.ResponseWriter, r *http.Request) {
    resp, _ := json.MarshalIndent(checker.Liveness(), "", "  ")
    w.Header().Add("Content-type", "application/json")
    w.Write(resp)
  })
  // declaring a endpoint to readiness
  http.HandleFunc("/health-check/readiness", func(w http.ResponseWriter, r *http.Request) {
    check := checker.Readiness()
    if !check.Status {
      // do something like write some log or call
      // other service passing full information
      // to handle this issue
    }
    resp, _ := json.MarshalIndent(check, "", "  ")
    w.Header().Add("Content-type", "application/json")
    w.Write(resp)
  })
  http.ListenAndServe(":8090", nil)
}

Migration guidelines

You will find all information you need in THIS DOC