Started the big refactor to extract common logic from engine package (Subsonic only) to core package (more generic)

This commit is contained in:
Deluan 2020-07-10 12:45:58 -04:00
parent 5418a6b6b1
commit 5620c58a30
25 changed files with 118 additions and 52 deletions

View file

@ -6,8 +6,9 @@
package cmd
import (
"github.com/deluan/navidrome/core"
"github.com/deluan/navidrome/core/transcoder"
"github.com/deluan/navidrome/engine"
"github.com/deluan/navidrome/engine/transcoder"
"github.com/deluan/navidrome/persistence"
"github.com/deluan/navidrome/scanner"
"github.com/deluan/navidrome/server"
@ -40,11 +41,11 @@ func CreateAppRouter() *app.Router {
func CreateSubsonicAPIRouter() (*subsonic.Router, error) {
dataStore := persistence.New()
browser := engine.NewBrowser(dataStore)
imageCache, err := engine.NewImageCache()
imageCache, err := core.NewImageCache()
if err != nil {
return nil, err
}
cover := engine.NewCover(dataStore, imageCache)
cover := core.NewCover(dataStore, imageCache)
nowPlayingRepository := engine.NewNowPlayingRepository()
listGenerator := engine.NewListGenerator(dataStore, nowPlayingRepository)
users := engine.NewUsers(dataStore)
@ -53,11 +54,11 @@ func CreateSubsonicAPIRouter() (*subsonic.Router, error) {
scrobbler := engine.NewScrobbler(dataStore, nowPlayingRepository)
search := engine.NewSearch(dataStore)
transcoderTranscoder := transcoder.New()
transcodingCache, err := engine.NewTranscodingCache()
transcodingCache, err := core.NewTranscodingCache()
if err != nil {
return nil, err
}
mediaStreamer := engine.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache)
mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache)
players := engine.NewPlayers(dataStore)
router := subsonic.New(browser, cover, listGenerator, users, playlists, ratings, scrobbler, search, mediaStreamer, players)
return router, nil
@ -65,4 +66,4 @@ func CreateSubsonicAPIRouter() (*subsonic.Router, error) {
// wire_injectors.go:
var allProviders = wire.NewSet(engine.Set, scanner.New, subsonic.New, app.New, persistence.New)
var allProviders = wire.NewSet(engine.Set, core.Set, scanner.New, subsonic.New, app.New, persistence.New)

View file

@ -3,6 +3,7 @@
package cmd
import (
"github.com/deluan/navidrome/core"
"github.com/deluan/navidrome/engine"
"github.com/deluan/navidrome/persistence"
"github.com/deluan/navidrome/scanner"
@ -14,6 +15,7 @@ import (
var allProviders = wire.NewSet(
engine.Set,
core.Set,
scanner.New,
subsonic.New,
app.New,

View file

@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/deluan/navidrome/engine/auth"
"github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/log"
"github.com/dgrijalva/jwt-go"
. "github.com/onsi/ginkgo"

15
core/common.go Normal file
View file

@ -0,0 +1,15 @@
package core
import (
"context"
"github.com/deluan/navidrome/model/request"
)
func userName(ctx context.Context) string {
if user, ok := request.UserFrom(ctx); !ok {
return "UNKNOWN"
} else {
return user.UserName
}
}

35
core/core_suite_test.go Normal file
View file

@ -0,0 +1,35 @@
package core
import (
"io/ioutil"
"os"
"testing"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/tests"
"github.com/djherbis/fscache"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestEngine(t *testing.T) {
tests.Init(t, false)
log.SetLevel(log.LevelCritical)
RegisterFailHandler(Fail)
RunSpecs(t, "Core Suite")
}
var testCache fscache.Cache
var testCacheDir string
var _ = Describe("Core Suite Setup", func() {
BeforeSuite(func() {
testCacheDir, _ = ioutil.TempDir("", "core_test_cache")
fs, _ := fscache.NewFs(testCacheDir, 0755)
testCache, _ = fscache.NewCache(fs, nil)
})
AfterSuite(func() {
os.RemoveAll(testCacheDir)
})
})

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"bytes"

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"bytes"

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"fmt"

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"io/ioutil"

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"context"
@ -10,7 +10,7 @@ import (
"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/engine/transcoder"
"github.com/deluan/navidrome/core/transcoder"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request"

View file

@ -1,4 +1,4 @@
package engine
package core
import (
"context"

View file

@ -0,0 +1,22 @@
package core
import "github.com/deluan/navidrome/model"
type mockTranscodingRepository struct {
model.TranscodingRepository
}
func (m *mockTranscodingRepository) Get(id string) (*model.Transcoding, error) {
return &model.Transcoding{ID: id, TargetFormat: "mp3", DefaultBitRate: 160}, nil
}
func (m *mockTranscodingRepository) FindByFormat(format string) (*model.Transcoding, error) {
switch format {
case "mp3":
return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil
case "oga":
return &model.Transcoding{ID: "oga1", TargetFormat: "oga", DefaultBitRate: 128}, nil
default:
return nil, model.ErrNotFound
}
}

14
core/wire_providers.go Normal file
View file

@ -0,0 +1,14 @@
package core
import (
"github.com/deluan/navidrome/core/transcoder"
"github.com/google/wire"
)
var Set = wire.NewSet(
NewCover,
NewMediaStreamer,
NewTranscodingCache,
NewImageCache,
transcoder.New,
)

View file

@ -1,13 +1,10 @@
package engine
import (
"io/ioutil"
"os"
"testing"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/tests"
"github.com/djherbis/fscache"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@ -18,18 +15,3 @@ func TestEngine(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Engine Suite")
}
var testCache fscache.Cache
var testCacheDir string
var _ = Describe("Engine Suite Setup", func() {
BeforeSuite(func() {
testCacheDir, _ = ioutil.TempDir("", "engine_test_cache")
fs, _ := fscache.NewFs(testCacheDir, 0755)
testCache, _ = fscache.NewCache(fs, nil)
})
AfterSuite(func() {
os.RemoveAll(testCacheDir)
})
})

View file

@ -7,7 +7,7 @@ import (
"fmt"
"strings"
"github.com/deluan/navidrome/engine/auth"
"github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/model"
)

View file

@ -3,7 +3,7 @@ package engine
import (
"context"
"github.com/deluan/navidrome/engine/auth"
"github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/persistence"
. "github.com/onsi/ginkgo"

View file

@ -1,13 +1,11 @@
package engine
import (
"github.com/deluan/navidrome/engine/transcoder"
"github.com/google/wire"
)
var Set = wire.NewSet(
NewBrowser,
NewCover,
NewListGenerator,
NewPlaylists,
NewRatings,
@ -15,9 +13,5 @@ var Set = wire.NewSet(
NewSearch,
NewNowPlayingRepository,
NewUsers,
NewMediaStreamer,
transcoder.New,
NewTranscodingCache,
NewImageCache,
NewPlayers,
)

View file

@ -8,7 +8,7 @@ import (
"github.com/deluan/navidrome/assets"
"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/engine/auth"
"github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/model"
"github.com/deluan/rest"
"github.com/go-chi/chi"

View file

@ -9,7 +9,7 @@ import (
"time"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/engine/auth"
"github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request"

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/deluan/navidrome/core"
"net/http"
"runtime"
@ -22,22 +23,22 @@ type Handler = func(http.ResponseWriter, *http.Request) (*responses.Subsonic, er
type Router struct {
Browser engine.Browser
Cover engine.Cover
Cover core.Cover
ListGenerator engine.ListGenerator
Playlists engine.Playlists
Ratings engine.Ratings
Scrobbler engine.Scrobbler
Search engine.Search
Users engine.Users
Streamer engine.MediaStreamer
Streamer core.MediaStreamer
Players engine.Players
mux http.Handler
}
func New(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGenerator, users engine.Users,
func New(browser engine.Browser, cover core.Cover, listGenerator engine.ListGenerator, users engine.Users,
playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search,
streamer engine.MediaStreamer, players engine.Players) *Router {
streamer core.MediaStreamer, players engine.Players) *Router {
r := &Router{Browser: browser, Cover: cover, ListGenerator: listGenerator, Playlists: playlists,
Ratings: ratings, Scrobbler: scrobbler, Search: search, Users: users, Streamer: streamer, Players: players}
r.mux = r.routes()

View file

@ -1,11 +1,11 @@
package subsonic
import (
"github.com/deluan/navidrome/core"
"io"
"net/http"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/engine"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/resources"
@ -14,10 +14,10 @@ import (
)
type MediaRetrievalController struct {
cover engine.Cover
cover core.Cover
}
func NewMediaRetrievalController(cover engine.Cover) *MediaRetrievalController {
func NewMediaRetrievalController(cover core.Cover) *MediaRetrievalController {
return &MediaRetrievalController{cover: cover}
}

View file

@ -1,21 +1,21 @@
package subsonic
import (
"github.com/deluan/navidrome/core"
"io"
"net/http"
"strconv"
"github.com/deluan/navidrome/engine"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
)
type StreamController struct {
streamer engine.MediaStreamer
streamer core.MediaStreamer
}
func NewStreamController(streamer engine.MediaStreamer) *StreamController {
func NewStreamController(streamer core.MediaStreamer) *StreamController {
return &StreamController{streamer: streamer}
}