51 lines
977 B
Go
51 lines
977 B
Go
package healthimp
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/api/http/response"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/api/routers/health"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/mlogger"
|
|
)
|
|
|
|
type Status struct {
|
|
logger mlogger.Logger
|
|
mu sync.RWMutex
|
|
status health.ServiceStatus
|
|
}
|
|
|
|
func (hs *Status) healthHandler() http.HandlerFunc {
|
|
hs.mu.RLock()
|
|
status := hs.status
|
|
hs.mu.RUnlock()
|
|
return response.Ok(hs.logger, struct {
|
|
Status health.ServiceStatus `json:"status"`
|
|
}{
|
|
status,
|
|
})
|
|
}
|
|
|
|
func (hr *Status) Finish() {
|
|
hr.logger.Info("Finished")
|
|
}
|
|
|
|
func (hs *Status) transition(status health.ServiceStatus) (health.ServiceStatus, bool) {
|
|
hs.mu.Lock()
|
|
defer hs.mu.Unlock()
|
|
previous := hs.status
|
|
if previous == status {
|
|
return previous, false
|
|
}
|
|
hs.status = status
|
|
return previous, true
|
|
}
|
|
|
|
func StatusHandler(logger mlogger.Logger) *Status {
|
|
hs := Status{
|
|
status: health.SSCreated,
|
|
logger: logger.Named("status"),
|
|
}
|
|
return &hs
|
|
}
|