status

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2021 License: MIT Imports: 8 Imported by: 0

README

Package Status

This package help to manage status router in a API to respond a standard response for status of API and his component. This package requires golib/router + go Gin Tonic API Framework.

Example of implementation

We will work on an example of file/folder tree like this :

/
  bin/
    api/
      config/
      routers/
        status/
          get.go

in the get.go file, we will implement the status package call :

package status

import (
    "github.com/gin-gonic/gin"
    "github.com/nabbar/golib/status"
    "github.com/nabbar/golib/version"
    "net/http"
)

const (
	msgOk = "API is working"
	msgKO = "something is not well working"
)

type EmptyStruct struct{}

var vers version.Version

func init() {
    // optional, get or create a new version interface
    vers = version.NewVersion(version.License_MIT, "Package", "Description", "2017-10-21T00:00:00+0200", "0123456789abcdef", "v0.0-dev", "Author Name", "pfx", EmptyStruct{}, 1)

    // to create new status, you will need some small function to give data, this is type func : 
    // FctMessagesAll   func() (ok string, ko string, cpt string)
    // FctMessageItem   func() (ok string, ko string)
    // FctHealth        func() error
    // FctInfo          func() (name, release, build string)
    // FctVersion       func() version.Version

    // create new status not as later
	sts := status.NewVersionStatus(getVersion, getMessageAll, GetHealth, GetHeader, false)
    // add a new component  
	sts.AddComponent(infoAws, getMessageItem, GetAWSHealth, true, false)
    // add a new component 
	sts.AddComponent(infoLDAP, getMessageItem, GetLDAPHealth, true, false) 
	
    // register to the router list
	sts.Register("/status", routers.RouterList.Register)
    
    // use this func to customize return code for each status
    sts.SetErrorCode(http.StatusOK, http.StatusInternalServerError, http.StatusAccepted)
}

func GetHealth() error {
    // any function to return check global API is up or not 
    return nil
}

func GetHeader(c *gin.Context) {
    // any function to return global & generic header (like CSP, HSTS, ...)
}

func GetAWSHealth() error {
    // any function to return the check of the component AWS is up or not 
    return nil
}

func GetLDAPHealth() error {
    // any function to return the check of the component LDAP is up or not 
    return nil
}

func infoAws() (name, release, build string) {
	return "AWS S3 Helper", "v0.1.2.3.4", ""
}

func infoLDAP() (name, release, build string) {
	return "OpenLDAP Lib", "v0.1.2.3.4", ""
}

func getVersion() version.Version {
    return vers
}

func getMessageItem() (ok string, ko string) {
  return "all is ok", "there is a mistake somewhere"
}

func getMessageAll() (ok string, ko string, cptErr string) {
  ok, ko = getMessageItem()
  return ok, ko, "at least one component is in failed"
}

In some case, using init function could make mistake (specially if you need to read flag or config file). In this case, you will have func "Later" to allow the init package on first call of status router.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FctHealth

type FctHealth func() error

type FctInfo

type FctInfo func() (name, release, build string)

type FctMessageItem

type FctMessageItem func() (ok string, ko string)

type FctMessagesAll

type FctMessagesAll func() (ok string, ko string, cpt string)

Model for function that return 3 string for message : ok : no error found for component and/or main. ko : error found for component and/or main. cpt : message for main status message only that say some of component are in error and are mandatory.

type FctVersion

type FctVersion func() version.Version

type Status

type Status interface {
	Register(prefix string, register router.RegisterRouter)
	RegisterGroup(group, prefix string, register router.RegisterRouterInGroup)

	AddComponent(componentRef string, info FctInfo, msg FctMessageItem, health FctHealth, WarnIfError bool, later bool)
	AddVersionComponent(componentRef string, vers FctVersion, msg FctMessageItem, health FctHealth, mandatoryComponent bool, later bool)
	DelComponent(componentRef string)

	Get(c *gin.Context)
	SetErrorCode(codeOk, codeKO, codeCptNoMandatoryKO int)
}

func NewStatus

func NewStatus(info FctInfo, msg FctMessagesAll, health FctHealth, Header gin.HandlerFunc, later bool) Status

func NewVersionStatus

func NewVersionStatus(vers FctVersion, msg FctMessagesAll, health FctHealth, Header gin.HandlerFunc, later bool) Status

type StatusItemResponse

type StatusItemResponse struct {
	Name      string
	Status    string
	Message   string
	Release   string
	HashBuild string
}

type StatusResponse

type StatusResponse struct {
	StatusItemResponse
	Component []StatusItemResponse
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL