Skip to content

Commit

Permalink
refactor: make health a module
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent 73c85a9 commit cef0dc0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
2 changes: 2 additions & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/JulesMike/spoty/build"
"github.com/JulesMike/spoty/cache"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/health"
"github.com/JulesMike/spoty/http"
"github.com/JulesMike/spoty/logger"
"github.com/JulesMike/spoty/spoty"
Expand All @@ -18,6 +19,7 @@ var Module = fx.Options(
config.Module,
logger.Module,
cache.Module,
health.Module,
http.Module,
spoty.Module,
fx.Invoke(bootstrap),
Expand Down
40 changes: 35 additions & 5 deletions health/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package health

import (
"context"
"sync"
"time"

"github.com/alexliesenfeld/health"
"go.uber.org/fx"
)

// Module exported for intialising the health checker.
var Module = fx.Options(
fx.Provide(New),
)

// Check represents a single health check.
Expand Down Expand Up @@ -37,15 +44,38 @@ type Check struct {
InitialDelay time.Duration
}

// CompileHealthCheckerOption takes a list of Check and returns health.CheckerOption.
func CompileHealthCheckerOption(checks ...Check) []health.CheckerOption {
var opts []health.CheckerOption
// Checks holds a list of Check from a list of health.Check.
type Checks struct {
mu sync.Mutex
items map[string]*Check
}

// New returns a new list of Check.
func New() *Checks {
return &Checks{
items: make(map[string]*Check),
}
}

// RegisterChecks registers a list of health.Check.
func (c *Checks) RegisterChecks(checks ...Check) {
for i := range checks {
c := &checks[i]
check := &checks[i]

if c.Name == "" {
if check.Name == "" {
continue
}

c.mu.Lock()
c.items[check.Name] = check
c.mu.Unlock()
}
}

// CompileHealthCheckerOption takes a list of Check and returns health.CheckerOption.
func (c *Checks) CompileHealthCheckerOption() []health.CheckerOption {
var opts []health.CheckerOption
for _, c := range c.items {
opts = append(opts, health.WithPeriodicCheck(c.RefreshPeriod, c.InitialDelay, health.Check{
Name: c.Name,
Timeout: c.Timeout,
Expand Down
3 changes: 1 addition & 2 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/JulesMike/spoty/docs"
"github.com/JulesMike/spoty/health"
ahealth "github.com/alexliesenfeld/health"
"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger"
Expand All @@ -30,7 +29,7 @@ type Error struct {
// @Success 503 {object} ahealth.CheckerResult
// @Router / [get]
func (s *Server) handleHealthCheck() gin.HandlerFunc {
opts := health.CompileHealthCheckerOption(s.spoty.Check())
opts := s.health.CompileHealthCheckerOption()
checker := ahealth.NewChecker(opts...)

return gin.WrapF(
Expand Down
4 changes: 4 additions & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/JulesMike/spoty/build"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/health"
"github.com/JulesMike/spoty/logger"
"github.com/JulesMike/spoty/spoty"
ginzap "github.com/gin-contrib/zap"
Expand All @@ -33,6 +34,7 @@ type Server struct {
http *http.Server
logger *logger.Logger
spoty *spoty.Spoty
health *health.Checks
build *build.Info
addr string
}
Expand All @@ -42,6 +44,7 @@ func New(
cfg *config.Config,
logger *logger.Logger,
spoty *spoty.Spoty,
health *health.Checks,
build *build.Info,
) *Server {
if cfg.Prod {
Expand All @@ -53,6 +56,7 @@ func New(
addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
logger: logger,
spoty: spoty,
health: health,
build: build,
}

Expand Down
24 changes: 13 additions & 11 deletions spoty/spoty.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ type Spoty struct {
auth spotify.Authenticator
state string

cache *cache.Cache
cache *cache.Cache
health *health.Checks
}

// New creates a new spoty service.
func New(cfg *config.Config, cache *cache.Cache) (*Spoty, error) {
func New(cfg *config.Config, cache *cache.Cache, health *health.Checks) (*Spoty, error) {
if cfg.ClientID == "" || cfg.ClientSecret == "" {
return nil, errors.New("missing clientID or clientSecret")
}

if cache == nil {
return nil, errors.New("invalid cache")
}

auth := spotify.NewAuthenticator(
fmt.Sprintf("http://%s:%d/api/callback", cfg.Host, cfg.Port),
spotify.ScopeUserReadCurrentlyPlaying,
Expand All @@ -72,11 +69,16 @@ func New(cfg *config.Config, cache *cache.Cache) (*Spoty, error) {
return nil, fmt.Errorf("new uuid: %w", err)
}

return &Spoty{
auth: auth,
state: state.String(),
cache: cache,
}, nil
spoty := Spoty{
auth: auth,
state: state.String(),
cache: cache,
health: health,
}

spoty.health.RegisterChecks(spoty.Check())

return &spoty, nil
}

// IsAuth returns true if the client was created.
Expand Down

0 comments on commit cef0dc0

Please sign in to comment.