Skip to content

Commit

Permalink
Merge pull request #29 from nabbar/Fix-pkg-Status
Browse files Browse the repository at this point in the history
Refactor Package Status
  • Loading branch information
Nicolas JUHEL authored Jul 22, 2020
2 parents 671e228 + 19c3e85 commit 865ff04
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 100 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ linters:
- gocritic
- stylecheck
- whitespace

- gofumpt
- nestif
- nolintlint

issues:
# List of regexps of issue texts to exclude, empty list by default.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666 h1:gVCS+QOncANNPlmlO1AhlU3oxs4V9z+gTtPwIk3p2N8=
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
56 changes: 48 additions & 8 deletions status/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,43 @@ package status

import (
"github.com/gin-gonic/gin"
"github.com/nabbar/golib/status"

"myapp/release"
"myapp/bin/api/config"
"myapp/bin/api/routers"
"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() {
sts := status.NewVersionStatus(release.GetVersion(), msgOk, msgKO, GetHealth, GetHeader)
// 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)
sts.AddComponent("AWS S3 Helper", msgOk, msgKO, "", "", true, GetAWSHealth)
sts.AddComponent("OpenLDAP", msgOk, msgKO, "", "", true, GetLDAPHealth)

// use this func to customize return code for each status
sts.SetErrorCode(http.StatusOK, http.StatusInternalServerError, http.StatusAccepted)
}

func GetHealth() error {
Expand All @@ -58,6 +78,26 @@ func GetLDAPHealth() error {
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).
Expand Down
Loading

0 comments on commit 865ff04

Please sign in to comment.