Skip to content

Commit

Permalink
Merge a7af130 into a6ed044
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 20, 2023
2 parents a6ed044 + a7af130 commit e8adcda
Show file tree
Hide file tree
Showing 28 changed files with 802 additions and 172 deletions.
14 changes: 7 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func runNavidrome() {

func startServer(ctx context.Context) func() error {
return func() error {
a := CreateServer(conf.Server.MusicFolder)
a.MountRouter("Native API", consts.URLPathNativeAPI, CreateNativeAPIRouter())
a.MountRouter("Subsonic API", consts.URLPathSubsonicAPI, CreateSubsonicAPIRouter())
a.MountRouter("Public Endpoints", consts.URLPathPublic, CreatePublicRouter())
a := CreateServer(ctx)
a.MountRouter("Native API", consts.URLPathNativeAPI, CreateNativeAPIRouter(ctx))
a.MountRouter("Subsonic API", consts.URLPathSubsonicAPI, CreateSubsonicAPIRouter(ctx))
a.MountRouter("Public Endpoints", consts.URLPathPublic, CreatePublicRouter(ctx))
if conf.Server.LastFM.Enabled {
a.MountRouter("LastFM Auth", consts.URLPathNativeAPI+"/lastfm", CreateLastFMRouter())
a.MountRouter("LastFM Auth", consts.URLPathNativeAPI+"/lastfm", CreateLastFMRouter(ctx))
}
if conf.Server.ListenBrainz.Enabled {
a.MountRouter("ListenBrainz Auth", consts.URLPathNativeAPI+"/listenbrainz", CreateListenBrainzRouter())
a.MountRouter("ListenBrainz Auth", consts.URLPathNativeAPI+"/listenbrainz", CreateListenBrainzRouter(ctx))
}
if conf.Server.Prometheus.Enabled {
// blocking call because takes <1ms but useful if fails
Expand All @@ -120,7 +120,7 @@ func schedulePeriodicScan(ctx context.Context) func() error {
return nil
}

scanner := GetScanner()
scanner := GetScanner(ctx)
schedulerInstance := scheduler.GetInstance()

log.Info("Scheduling periodic scan", "schedule", schedule)
Expand Down
5 changes: 3 additions & 2 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ var scanCmd = &cobra.Command{
}

func runScanner() {
scanner := GetScanner()
_ = scanner.RescanAll(context.Background(), fullRescan)
ctx := context.Background()
scanner := GetScanner(ctx)
_ = scanner.RescanAll(ctx, fullRescan)
if fullRescan {
log.Info("Finished full rescan")
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/signaler_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const triggerScanSignal = syscall.SIGUSR1

func startSignaler(ctx context.Context) func() error {
log.Info(ctx, "Starting signaler")
scanner := GetScanner()
scanner := GetScanner(ctx)

return func() error {
var sigChan = make(chan os.Signal, 1)
Expand Down
32 changes: 13 additions & 19 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions cmd/wire_injectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"context"
"sync"

"github.com/google/wire"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner"
"github.com/navidrome/navidrome/scanner2"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/nativeapi"
Expand All @@ -33,39 +35,39 @@ var allProviders = wire.NewSet(
db.Db,
)

func CreateServer(musicFolder string) *server.Server {
func CreateServer(ctx context.Context) *server.Server {
panic(wire.Build(
server.New,
allProviders,
))
}

func CreateNativeAPIRouter() *nativeapi.Router {
func CreateNativeAPIRouter(ctx context.Context) *nativeapi.Router {
panic(wire.Build(
allProviders,
))
}

func CreateSubsonicAPIRouter() *subsonic.Router {
func CreateSubsonicAPIRouter(ctx context.Context) *subsonic.Router {
panic(wire.Build(
allProviders,
GetScanner,
))
}

func CreatePublicRouter() *public.Router {
func CreatePublicRouter(ctx context.Context) *public.Router {
panic(wire.Build(
allProviders,
))
}

func CreateLastFMRouter() *lastfm.Router {
func CreateLastFMRouter(ctx context.Context) *lastfm.Router {
panic(wire.Build(
allProviders,
))
}

func CreateListenBrainzRouter() *listenbrainz.Router {
func CreateListenBrainzRouter(ctx context.Context) *listenbrainz.Router {
panic(wire.Build(
allProviders,
))
Expand All @@ -77,16 +79,16 @@ var (
scannerInstance scanner.Scanner
)

func GetScanner() scanner.Scanner {
func GetScanner(ctx context.Context) scanner.Scanner {
onceScanner.Do(func() {
scannerInstance = createScanner()
scannerInstance = createScanner(ctx)
})
return scannerInstance
}

func createScanner() scanner.Scanner {
func createScanner(ctx context.Context) scanner.Scanner {
panic(wire.Build(
allProviders,
scanner.New,
scanner2.New,
))
}
72 changes: 72 additions & 0 deletions db/migration/20231212210020_add_library_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package migrations

import (
"context"
"database/sql"
"fmt"

"github.com/navidrome/navidrome/conf"
"github.com/pressly/goose/v3"
)

func init() {
goose.AddMigrationContext(upAddLibraryTable, downAddLibraryTable)
}

func upAddLibraryTable(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
create table library (
id integer primary key autoincrement,
name varchar not null unique,
path varchar not null unique,
remote_path varchar null default '',
extractor varchar null default 'taglib',
last_scan_at datetime not null default '0000-00-00 00:00:00',
updated_at datetime not null default current_timestamp,
created_at datetime not null default current_timestamp
);`)
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, fmt.Sprintf(`
insert into library(id, name, path, extractor, last_scan_at) values(1, 'Music Library', '%s', '%s', current_timestamp);
delete from property where id like 'LastScan-%%';
`, conf.Server.MusicFolder, conf.Server.Scanner.Extractor))
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, `
alter table media_file add column library_id integer not null default 1
references library(id) on delete cascade;
alter table album add column library_id integer not null default 1
references library(id) on delete cascade;
create table if not exists library_artist
(
library_id integer not null default 1
references library(id)
on delete cascade,
artist_id varchar not null default null
references artist(id)
on delete cascade,
constraint library_artist_ux
unique (library_id, artist_id)
);
insert into library_artist(library_id, artist_id) select 1, id from artist;
`)

return err
}

func downAddLibraryTable(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
alter table media_file drop column library_id;
alter table album drop column library_id;
drop table library_artist;
drop table library;
`)
return err
}
38 changes: 38 additions & 0 deletions db/migration/20231219003407_add_folder_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package migrations

import (
"context"
"database/sql"

"github.com/pressly/goose/v3"
)

func init() {
goose.AddMigrationContext(upAddFolderTable, downAddFolderTable)
}

func upAddFolderTable(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
create table if not exists folder(
id varchar not null
primary key,
library_id integer not null
references library (id)
on delete cascade,
path varchar default '' not null,
name varchar default '' not null,
updated_at timestamp default current_timestamp not null,
created_at timestamp default current_timestamp not null,
parent_id varchar default null
references folder (id)
on delete cascade
);
`)

return err
}

func downAddFolderTable(ctx context.Context, tx *sql.Tx) error {
// This code is executed when the migration is rolled back.
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/squirrel v1.5.4
github.com/ReneKroon/ttlcache/v2 v2.11.0
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
github.com/charlievieth/fastwalk v1.0.1
github.com/deluan/rest v0.0.0-20211102003136-6260bc399cbf
github.com/deluan/sanitize v0.0.0-20230310221930-6e18967d9fc1
github.com/dexterlb/mpvipc v0.0.0-20230829142118-145d6eabdc37
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1l
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/charlievieth/fastwalk v1.0.1 h1:jW01w8OCFdKS9JvAcnI+JHhWU/FuIEmNb24Ri9p7OVg=
github.com/charlievieth/fastwalk v1.0.1/go.mod h1:dryXgMJyGHbMrAmmnF0/EJNBbZaihlwcNud5IuGyogU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -228,6 +230,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
3 changes: 2 additions & 1 deletion model/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ type ResourceRepository interface {
}

type DataStore interface {
Library(ctx context.Context) LibraryRepository
Folder(ctx context.Context) FolderRepository
Album(ctx context.Context) AlbumRepository
Artist(ctx context.Context) ArtistRepository
MediaFile(ctx context.Context) MediaFileRepository
MediaFolder(ctx context.Context) MediaFolderRepository
Genre(ctx context.Context) GenreRepository
Playlist(ctx context.Context) PlaylistRepository
PlayQueue(ctx context.Context) PlayQueueRepository
Expand Down

0 comments on commit e8adcda

Please sign in to comment.