Skip to content

Commit

Permalink
Merge pull request #17 from nabbar/status-on-init
Browse files Browse the repository at this point in the history
Add features on Status / Static
  • Loading branch information
Nicolas JUHEL authored Jul 8, 2020
2 parents 1663938 + 266ea71 commit 9570840
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 21 deletions.
34 changes: 30 additions & 4 deletions njs-router/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ type Headers interface {
}

func NewHeaders() Headers {
return &headers{}
return &headers{
head: make(http.Header, 0),
}
}

func (h headers) Clone() Headers {
Expand All @@ -69,6 +71,10 @@ func (h headers) Register(router ...gin.HandlerFunc) []gin.HandlerFunc {
func (h headers) Header() map[string]string {
res := make(map[string]string)

if h.head == nil {
return res
}

for k := range h.head {
res[k] = h.head.Get(k)
}
Expand All @@ -77,21 +83,33 @@ func (h headers) Header() map[string]string {
}

func (h headers) Handler(c *gin.Context) {
if h.head == nil {
return
}

for k := range h.head {
c.Header(k, h.head.Get(k))
}
}

// Add adds the key, value pair to the header.
// It appends to any existing values associated with key.
func (h headers) Add(key, value string) {
func (h *headers) Add(key, value string) {
if h.head == nil {
h.head = make(http.Header, 0)
}

h.head.Add(key, value)
}

// Set sets the header entries associated with key to
// the single element value. It replaces any existing
// values associated with key.
func (h headers) Set(key, value string) {
func (h *headers) Set(key, value string) {
if h.head == nil {
h.head = make(http.Header, 0)
}

h.head.Set(key, value)
}

Expand All @@ -102,10 +120,18 @@ func (h headers) Set(key, value string) {
// To access multiple values of a key, or to use non-canonical keys,
// access the map directly.
func (h headers) Get(key string) string {
if h.head == nil {
h.head = make(http.Header, 0)
}

return h.head.Get(key)
}

// Del deletes the values associated with key.
func (h headers) Del(key string) {
func (h *headers) Del(key string) {
if h.head == nil {
h.head = make(http.Header, 0)
}

h.head.Del(key)
}
7 changes: 4 additions & 3 deletions njs-router/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/gin-gonic/gin"
)

const _EMPTY_GROUP = "<nil>"
const EMPTY_GROUP = "<nil>"

var (
defaultRouters = NewRouterList()
Expand All @@ -46,6 +46,7 @@ type routerList struct {
}

type RegisterRouter func(method string, relativePath string, router ...gin.HandlerFunc)
type RegisterRouterInGroup func(group, method string, relativePath string, router ...gin.HandlerFunc)

type RouterList interface {
Register(method string, relativePath string, router ...gin.HandlerFunc)
Expand Down Expand Up @@ -73,7 +74,7 @@ func NewRouterList() RouterList {

func (l routerList) Handler(engine *gin.Engine) {
for grpRoute, grpList := range l.list {
if grpRoute == _EMPTY_GROUP {
if grpRoute == EMPTY_GROUP {
for _, r := range grpList {
engine.Handle(r.method, r.relative, r.router...)
}
Expand All @@ -88,7 +89,7 @@ func (l routerList) Handler(engine *gin.Engine) {

func (l *routerList) RegisterInGroup(group, method string, relativePath string, router ...gin.HandlerFunc) {
if group == "" {
group = _EMPTY_GROUP
group = EMPTY_GROUP
}

if _, ok := l.list[group]; !ok {
Expand Down
27 changes: 24 additions & 3 deletions njs-static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,33 @@ type staticHandler struct {

type Static interface {
Register(register njs_router.RegisterRouter)
RegisterInGroup(group string, register njs_router.RegisterRouterInGroup)

SetDownloadAll()
SetDownload(file string)
IsDownload(file string) bool

Has(file string) bool
Find(file string) ([]byte, error)

Health() error
Get(c *gin.Context)
}

func cleanPath(p string) string {
return filepath.Clean(filepath.Join("/", strings.TrimLeft(p, "/")))
}

func cleanJoinPath(p, e string) string {
return cleanPath(filepath.Join(strings.TrimLeft(p, "/"), e))
}

func NewStatic(hasIndex bool, prefix string, box packr.Box, Header func() map[string]string) Static {
return &staticHandler{
box: box,
debug: false,
index: hasIndex,
prefix: "/" + strings.Trim(prefix, "/"),
prefix: cleanPath(prefix),
head: Header,
download: make([]string, 0),
}
Expand All @@ -84,11 +94,22 @@ func NewStatic(hasIndex bool, prefix string, box packr.Box, Header func() map[st
func (s staticHandler) Register(register njs_router.RegisterRouter) {
if s.prefix == "/" {
for _, f := range s.box.List() {
register(http.MethodGet, s.prefix+f, s.Get)
register(http.MethodGet, cleanJoinPath(s.prefix, f), s.Get)
}
} else {
register(http.MethodGet, s.prefix, s.Get)
register(http.MethodGet, s.prefix+"/*file", s.Get)
register(http.MethodGet, cleanJoinPath(s.prefix, "/*file"), s.Get)
}
}

func (s staticHandler) RegisterInGroup(group string, register njs_router.RegisterRouterInGroup) {
if s.prefix == "/" {
for _, f := range s.box.List() {
register(group, http.MethodGet, cleanJoinPath(s.prefix, f), s.Get)
}
} else {
register(group, http.MethodGet, s.prefix, s.Get)
register(group, http.MethodGet, cleanJoinPath(s.prefix, "/*file"), s.Get)
}
}

Expand Down
164 changes: 153 additions & 11 deletions njs-status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package njs_status
import (
"fmt"
"net/http"
"path"
"strings"

njs_router "github.com/nabbar/golib/njs-router"
Expand Down Expand Up @@ -65,26 +66,71 @@ type statusItem struct {
type statusPartner struct {
statusItem
WarnIfErr bool
later *initLater
}

type mainPackage struct {
statusItem
ptn []statusPartner
header func(c *gin.Context)
later *initLater
init bool
}

type initLater struct {
version func() njs_version.Version
info func() (name, release, build string)
msg func() (ok string, ko string)
health func() error
}

type Status interface {
Register(prefix string, register njs_router.RegisterRouter)

AddPartner(name, msgOK, msgKO, release, build string, WarnIfError bool, health func() error)
AddVersionPartner(vers njs_version.Version, msgOK, msgKO string, WarnIfError bool, health func() error)

LaterAddPartner(info func() (name, release, build string), msg func() (ok string, ko string), health func() error, WarnIfError bool)
LaterAddVersionPartner(vers func() njs_version.Version, msg func() (ok string, ko string), health func() error, WarnIfError bool)

Get(c *gin.Context)
}

func NewStatusLater(info func() (name, release, build string), msg func() (ok string, ko string), health func() error, Header func(c *gin.Context)) Status {
return &mainPackage{
ptn: make([]statusPartner, 0),
header: Header,
later: &initLater{
version: nil,
info: info,
msg: msg,
health: health,
},
init: false,
}
}

func NewStatus(name, msgOK, msgKO, release, build string, health func() error, Header func(c *gin.Context)) Status {
return &mainPackage{
newItem(name, msgOK, msgKO, release, build, health),
make([]statusPartner, 0),
Header,
statusItem: newItem(name, msgOK, msgKO, release, build, health),
ptn: make([]statusPartner, 0),
header: Header,
later: nil,
init: false,
}
}

func NewVersionStatusLater(vers func() njs_version.Version, msg func() (ok string, ko string), health func() error, Header func(c *gin.Context)) Status {
return &mainPackage{
ptn: make([]statusPartner, 0),
header: Header,
later: &initLater{
version: vers,
info: nil,
msg: msg,
health: health,
},
init: false,
}
}

Expand All @@ -105,26 +151,118 @@ func newItem(name, msgOK, msgKO, release, build string, health func() error) sta

func (p *mainPackage) AddPartner(name, msgOK, msgKO, release, build string, WarnIfError bool, health func() error) {
p.ptn = append(p.ptn, statusPartner{
newItem(name, msgOK, msgKO, release, build, health),
WarnIfError,
statusItem: newItem(name, msgOK, msgKO, release, build, health),
WarnIfErr: WarnIfError,
later: nil,
})
}

func (p *mainPackage) LaterAddPartner(info func() (name, release, build string), msg func() (ok string, ko string), health func() error, WarnIfError bool) {
p.ptn = append(p.ptn, statusPartner{
WarnIfErr: WarnIfError,
later: &initLater{
version: nil,
info: info,
msg: msg,
health: health,
},
})
}

func (p *mainPackage) AddVersionPartner(vers njs_version.Version, msgOK, msgKO string, WarnIfError bool, health func() error) {
p.AddPartner(vers.GetPackage(), msgOK, msgKO, vers.GetRelease(), vers.GetBuild(), WarnIfError, health)
}

func (s mainPackage) Register(prefix string, register njs_router.RegisterRouter) {
prefix = "/" + strings.Trim(prefix, "/")
func (p *mainPackage) LaterAddVersionPartner(vers func() njs_version.Version, msg func() (ok string, ko string), health func() error, WarnIfError bool) {
p.ptn = append(p.ptn, statusPartner{
WarnIfErr: WarnIfError,
later: &initLater{
version: vers,
info: nil,
msg: msg,
health: health,
},
})
}

func (p *mainPackage) initStatus() {
if p.later != nil {
if p.later.info != nil {
name, release, build := p.later.info()
ok, ko := p.later.msg()
p.statusItem = newItem(name, ok, ko, release, build, p.health)
} else if p.later.version != nil {
vers := p.later.version()
ok, ko := p.later.msg()
p.statusItem = newItem(vers.GetPackage(), ok, ko, vers.GetRelease(), vers.GetBuild(), p.health)
}

if p.later.health != nil {
p.health = p.later.health
}

p.later = nil
}

var ptn = make([]statusPartner, 0)

for _, part := range p.ptn {
if part.later != nil {
if part.later.info != nil {
name, release, build := part.later.info()
ok, ko := part.later.msg()
part = statusPartner{
statusItem: newItem(name, ok, ko, release, build, part.health),
WarnIfErr: part.WarnIfErr,
later: nil,
}
} else if p.later.version != nil {
v := p.later.version()
ok, ko := p.later.msg()
part = statusPartner{
statusItem: newItem(v.GetPackage(), ok, ko, v.GetRelease(), v.GetBuild(), part.health),
WarnIfErr: part.WarnIfErr,
later: nil,
}
}

if part.later.health != nil {
part.health = part.later.health
}
}

ptn = append(ptn, part)
}

p.init = true
p.ptn = ptn
}

func (p *mainPackage) cleanPrefix(prefix string) string {
return path.Clean(strings.TrimRight(path.Join("/", prefix), "/"))
}

func (p *mainPackage) Register(prefix string, register njs_router.RegisterRouter) {
prefix = p.cleanPrefix(prefix)

register(http.MethodGet, prefix, p.header, p.Get)

if prefix != "/" {
register(http.MethodGet, prefix+"/", p.header, p.Get)
}
}

func (p *mainPackage) RegisterGroup(group, prefix string, register njs_router.RegisterRouterInGroup) {
prefix = p.cleanPrefix(prefix)

register(http.MethodGet, prefix, s.header, s.Get)
register(group, http.MethodGet, prefix, p.header, p.Get)

if prefix != "/" {
register(http.MethodGet, prefix+"/", s.header, s.Get)
register(group, http.MethodGet, prefix+"/", p.header, p.Get)
}
}

func (p statusItem) GetStatusResponse(c *gin.Context) StatusItemResponse {
func (p *statusItem) GetStatusResponse(c *gin.Context) StatusItemResponse {
res := StatusItemResponse{
Name: p.name,
Status: statusOK,
Expand Down Expand Up @@ -153,7 +291,11 @@ func (p statusItem) GetStatusResponse(c *gin.Context) StatusItemResponse {
return res
}

func (p mainPackage) Get(c *gin.Context) {
func (p *mainPackage) Get(c *gin.Context) {
if !p.init {
p.initStatus()
}

hasError := false
res := StatusResponse{
p.GetStatusResponse(c),
Expand Down

0 comments on commit 9570840

Please sign in to comment.