Skip to content

Commit

Permalink
refactor: proper use of fx
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent 8532fc6 commit 2dea33f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 99 deletions.
10 changes: 0 additions & 10 deletions app/app.go

This file was deleted.

54 changes: 0 additions & 54 deletions app/provider.go

This file was deleted.

37 changes: 37 additions & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package bootstrap

import (
"context"

"github.com/JulesMike/spoty/cache"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/server"
"github.com/JulesMike/spoty/spoty"
"go.uber.org/fx"
)

// Module exported for initialising application.
var Module = fx.Options(
config.Module,
cache.Module,
server.Module,
spoty.Module,
fx.Invoke(bootstrap),
)

func bootstrap(lc fx.Lifecycle, s *server.Server) error {
s.RegisterRoutes()

lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
go s.Start()

return nil
},
OnStop: func(ctx context.Context) error {
return s.Stop(ctx)
},
})

return nil
}
12 changes: 9 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package cache

import (
"github.com/JulesMike/spoty/config"
"github.com/dgraph-io/ristretto"
jsoniter "github.com/json-iterator/go"
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(New),
)

type Cache struct {
*ristretto.Cache
}

func New(maxKeys, maxCost int64) (*Cache, error) {
func New(cfg *config.Config) (*Cache, error) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: maxKeys,
MaxCost: maxCost,
NumCounters: cfg.CacheMaxKeys,
MaxCost: cfg.CacheMaxCost,
BufferItems: 64,
Cost: func(value interface{}) int64 {
test, err := jsoniter.Marshal(value)
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"fmt"

"github.com/kelseyhightower/envconfig"
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(New),
)

type Config struct {
Expand Down
23 changes: 2 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package main

import (
"log"

"github.com/JulesMike/spoty/app"
"github.com/JulesMike/spoty/server"
"github.com/JulesMike/spoty/bootstrap"
"go.uber.org/fx"
)

Expand All @@ -18,21 +15,5 @@ import (
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

func main() {
if err := run(); err != nil {
log.Fatalf("[Spoty] %v", err)
}
}

func run() error {
app := fx.New(
app.DefaultProviders,
fx.Invoke(func(server *server.Server) {}),
)
if err := app.Err(); err != nil {
return err
}

app.Run()

return nil
fx.New(bootstrap.Module).Run()
}
16 changes: 10 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"net/http"
"time"

"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/spoty"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(New),
)

type Server struct {
Expand All @@ -18,14 +24,14 @@ type Server struct {
addr string
}

func New(prod bool, host string, port int, spoty *spoty.Spoty) *Server {
if prod {
func New(cfg *config.Config, spoty *spoty.Spoty) *Server {
if cfg.Prod {
gin.SetMode(gin.ReleaseMode)
}

s := Server{
router: gin.Default(),
addr: fmt.Sprintf("%s:%d", host, port),
addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
spoty: spoty,
}

Expand All @@ -38,12 +44,10 @@ func New(prod bool, host string, port int, spoty *spoty.Spoty) *Server {
ReadHeaderTimeout: 2 * time.Second,
}

s.registerRoutes()

return &s
}

func (s *Server) registerRoutes() {
func (s *Server) RegisterRoutes() {
// Swagger
s.router.GET("/swagger/*any", s.handleSwagger())

Expand Down
16 changes: 11 additions & 5 deletions spoty/spoty.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ import (
"time"

"github.com/JulesMike/spoty/cache"
"github.com/JulesMike/spoty/config"
"github.com/cenkalti/dominantcolor"
"github.com/google/uuid"
"github.com/iancoleman/strcase"
"github.com/zmb3/spotify"
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(New),
)

type Image struct {
Expand All @@ -36,8 +42,8 @@ type Spoty struct {
cache *cache.Cache
}

func New(clientID, clientSecret, host string, port int, cache *cache.Cache) (*Spoty, error) {
if clientID == "" || clientSecret == "" {
func New(cfg *config.Config, cache *cache.Cache) (*Spoty, error) {
if cfg.ClientID == "" || cfg.ClientSecret == "" {
return nil, errors.New("missing clientID or clientSecret")
}

Expand All @@ -46,12 +52,12 @@ func New(clientID, clientSecret, host string, port int, cache *cache.Cache) (*Sp
}

auth := spotify.NewAuthenticator(
fmt.Sprintf("http://%s:%d/api/callback", host, port),
fmt.Sprintf("http://%s:%d/api/callback", cfg.Host, cfg.Port),
spotify.ScopeUserReadCurrentlyPlaying,
spotify.ScopeUserReadPlaybackState,
)

auth.SetAuthInfo(clientID, clientSecret)
auth.SetAuthInfo(cfg.ClientID, cfg.ClientSecret)

state, err := uuid.NewRandom()
if err != nil {
Expand Down Expand Up @@ -122,7 +128,7 @@ func (s *Spoty) TrackImages(track *spotify.FullTrack) ([]Image, error) {
return nil, errors.New("invalid track")
}

var cacheTrackImagesKey = "track_" + strcase.ToCamel(string(track.ID)) + "_images"
cacheTrackImagesKey := "track_" + strcase.ToCamel(string(track.ID)) + "_images"

cachedImages, found := s.cache.Get(cacheTrackImagesKey)
if found {
Expand Down

0 comments on commit 2dea33f

Please sign in to comment.