diff --git a/conf/configuration.go b/conf/configuration.go index 9652d13f..e50d86ee 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -28,8 +28,8 @@ type nd struct { ScanInterval string `default:"1m"` // DevFlags. These are used to enable/disable debugging and incomplete features - DevDisableAuthentication bool `default:"false"` - DevDisableBanner bool `default:"false"` + DevDisableBanner bool `default:"false"` + DevLogSourceLine bool `default:"false"` } var Server = &nd{} @@ -85,7 +85,8 @@ func LoadFromFile(confFile string, skipFlags ...bool) { if os.Getenv("PORT") != "" { Server.Port = os.Getenv("PORT") } - log.SerLevelString(Server.LogLevel) + log.SetLevelString(Server.LogLevel) + log.SetLogSourceLine(Server.DevLogSourceLine) log.Trace("Loaded configuration", "file", confFile, "config", fmt.Sprintf("%#v", Server)) } diff --git a/db/migrations/schema.go b/db/migrations/schema.go index 7f2f5155..df338f40 100644 --- a/db/migrations/schema.go +++ b/db/migrations/schema.go @@ -1,136 +1,165 @@ package migrations var schema = ` -create table if not exists media_file +create table album ( - id varchar(255) not null - primary key, - title varchar(255) not null, - album varchar(255) default '' not null, - artist varchar(255) default '' not null, - artist_id varchar(255) default '' not null, - album_artist varchar(255) default '' not null, - album_id varchar(255) default '' not null, - has_cover_art bool default FALSE not null, - track_number integer default 0 not null, - disc_number integer default 0 not null, - year integer default 0 not null, - size integer default 0 not null, - path varchar(1024) not null, - suffix varchar(255) default '' not null, - duration integer default 0 not null, - bit_rate integer default 0 not null, - genre varchar(255) default '' not null, - compilation bool default FALSE not null, - created_at datetime, - updated_at datetime + id varchar(255) not null + primary key, + name varchar(255) default '' not null, + artist_id varchar(255) default '' not null, + cover_art_path varchar(255) default '' not null, + cover_art_id varchar(255) default '' not null, + artist varchar(255) default '' not null, + album_artist varchar(255) default '' not null, + year integer default 0 not null, + compilation bool default FALSE not null, + song_count integer default 0 not null, + duration integer default 0 not null, + genre varchar(255) default '' not null, + created_at datetime, + updated_at datetime ); -create index if not exists media_file_title - on media_file (title); +create index album_artist + on album (artist); -create index if not exists media_file_album_id - on media_file (album_id); +create index album_artist_id + on album (artist_id); -create index if not exists media_file_album - on media_file (album); +create index album_genre + on album (genre); -create index if not exists media_file_artist_id - on media_file (artist_id); +create index album_name + on album (name); -create index if not exists media_file_artist - on media_file (artist); +create index album_year + on album (year); -create index if not exists media_file_album_artist - on media_file (album_artist); - -create index if not exists media_file_genre - on media_file (genre); - -create index if not exists media_file_year - on media_file (year); - -create index if not exists media_file_compilation - on media_file (compilation); - -create index if not exists media_file_path - on media_file (path); - -create table if not exists annotation +create table annotation ( - ann_id varchar(255) not null - primary key, - user_id varchar(255) default '' not null, - item_id varchar(255) default '' not null, - item_type varchar(255) default '' not null, - play_count integer, - play_date datetime, - rating integer, - starred bool default FALSE not null, - starred_at datetime, - unique (user_id, item_id, item_type) + ann_id varchar(255) not null + primary key, + user_id varchar(255) default '' not null, + item_id varchar(255) default '' not null, + item_type varchar(255) default '' not null, + play_count integer, + play_date datetime, + rating integer, + starred bool default FALSE not null, + starred_at datetime, + unique (user_id, item_id, item_type) ); -create index if not exists annotation_play_count - on annotation (play_count); +create index annotation_play_count + on annotation (play_count); -create index if not exists annotation_play_date - on annotation (play_date); +create index annotation_play_date + on annotation (play_date); -create index if not exists annotation_starred - on annotation (starred); +create index annotation_rating + on annotation (rating); -create table if not exists playlist +create index annotation_starred + on annotation (starred); + +create table artist ( - id varchar(255) not null - primary key, - name varchar(255) not null, - comment varchar(255) default '' not null, - duration integer default 0 not null, - owner varchar(255) default '' not null, - public bool default FALSE not null, - tracks text not null, - unique (owner, name) + id varchar(255) not null + primary key, + name varchar(255) default '' not null, + album_count integer default 0 not null ); -create index if not exists playlist_name - on playlist (name); +create index artist_name + on artist (name); -create table if not exists property +create table media_file ( - id varchar(255) not null - primary key, - value varchar(1024) default '' not null + id varchar(255) not null + primary key, + path varchar(255) default '' not null, + title varchar(255) default '' not null, + album varchar(255) default '' not null, + artist varchar(255) default '' not null, + artist_id varchar(255) default '' not null, + album_artist varchar(255) default '' not null, + album_id varchar(255) default '' not null, + has_cover_art bool default FALSE not null, + track_number integer default 0 not null, + disc_number integer default 0 not null, + year integer default 0 not null, + size integer default 0 not null, + suffix varchar(255) default '' not null, + duration integer default 0 not null, + bit_rate integer default 0 not null, + genre varchar(255) default '' not null, + compilation bool default FALSE not null, + created_at datetime, + updated_at datetime ); -create table if not exists search +create index media_file_album_id + on media_file (album_id); + +create index media_file_genre + on media_file (genre); + +create index media_file_path + on media_file (path); + +create index media_file_title + on media_file (title); + +create table playlist ( - id varchar(255) not null - primary key, - "table" varchar(255) not null, - full_text varchar(1024) not null + id varchar(255) not null + primary key, + name varchar(255) default '' not null, + comment varchar(255) default '' not null, + duration integer default 0 not null, + owner varchar(255) default '' not null, + public bool default FALSE not null, + tracks text not null ); -create index if not exists search_full_text - on search (full_text); +create index playlist_name + on playlist (name); -create index if not exists search_table - on search ("table"); - -create table if not exists user +create table property ( - id varchar(255) not null - primary key, - user_name varchar(255) default '' not null - unique, - name varchar(255) default '' not null, - email varchar(255) default '' not null - unique, - password varchar(255) default '' not null, - is_admin bool default FALSE not null, - last_login_at datetime, - last_access_at datetime, - created_at datetime not null, - updated_at datetime not null + id varchar(255) not null + primary key, + value varchar(255) default '' not null +); + +create table search +( + id varchar(255) not null + primary key, + "table" varchar(255) default '' not null, + full_text varchar(255) default '' not null +); + +create index search_full_text + on search (full_text); + +create index search_table + on search ("table"); + +create table user +( + id varchar(255) not null + primary key, + user_name varchar(255) default '' not null + unique, + name varchar(255) default '' not null, + email varchar(255) default '' not null + unique, + password varchar(255) default '' not null, + is_admin bool default FALSE not null, + last_login_at datetime, + last_access_at datetime, + created_at datetime not null, + updated_at datetime not null ); ` diff --git a/log/log.go b/log/log.go index da50fde3..ceffefe1 100644 --- a/log/log.go +++ b/log/log.go @@ -27,6 +27,7 @@ const ( var ( currentLevel Level defaultLogger = logrus.New() + logSourceLine = false ) // SetLevel sets the global log level used by the simple logger. @@ -35,7 +36,7 @@ func SetLevel(l Level) { logrus.SetLevel(logrus.Level(l)) } -func SerLevelString(l string) { +func SetLevelString(l string) { envLevel := strings.ToLower(l) var level Level switch envLevel { @@ -55,6 +56,10 @@ func SerLevelString(l string) { SetLevel(level) } +func SetLogSourceLine(enabled bool) { + logSourceLine = enabled +} + func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Context { if ctx == nil { ctx = context.Background() @@ -132,7 +137,7 @@ func parseArgs(args []interface{}) (*logrus.Entry, string) { kvPairs := args[1:] l = addFields(l, kvPairs) } - if currentLevel >= LevelTrace { + if logSourceLine { _, file, line, ok := runtime.Caller(2) if !ok { file = "???" diff --git a/persistence/sql_repository.go b/persistence/sql_repository.go index 900f58c6..c8212483 100644 --- a/persistence/sql_repository.go +++ b/persistence/sql_repository.go @@ -143,7 +143,18 @@ func (r sqlRepository) delete(cond Sqlizer) error { func (r sqlRepository) toSql(sq Sqlizer) (string, []interface{}, error) { sql, args, err := sq.ToSql() if err == nil { - log.Trace(r.ctx, "SQL: `"+sql+"`", "args", strings.TrimPrefix(fmt.Sprintf("%#v", args), "[]interface {}")) + var fmtArgs []string + for i := range args { + var f string + switch a := args[i].(type) { + case string: + f = `'` + a + `'` + default: + f = fmt.Sprintf("%v", a) + } + fmtArgs = append(fmtArgs, f) + } + log.Trace(r.ctx, "SQL: `"+sql+"`", "args", `[`+strings.Join(fmtArgs, ",")+`]`) } return sql, args, err } diff --git a/server/app/app.go b/server/app/app.go index 1ed9eb4e..1cca3ebb 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/deluan/navidrome/assets" - "github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/model" "github.com/deluan/rest" "github.com/go-chi/chi" @@ -46,10 +45,8 @@ func (app *Router) routes() http.Handler { r.Post("/createAdmin", CreateAdmin(app.ds)) r.Route("/api", func(r chi.Router) { - if !conf.Server.DevDisableAuthentication { - r.Use(jwtauth.Verifier(TokenAuth)) - r.Use(Authenticator(app.ds)) - } + r.Use(jwtauth.Verifier(TokenAuth)) + r.Use(Authenticator(app.ds)) app.R(r, "/user", model.User{}) app.R(r, "/song", model.MediaFile{}) app.R(r, "/album", model.Album{}) diff --git a/server/subsonic/api.go b/server/subsonic/api.go index a2f6a431..ac2075ca 100644 --- a/server/subsonic/api.go +++ b/server/subsonic/api.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" - "github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/engine" "github.com/deluan/navidrome/server/subsonic/responses" "github.com/go-chi/chi" @@ -48,11 +47,9 @@ func (api *Router) routes() http.Handler { r.Use(postFormToQueryParams) r.Use(checkRequiredParameters) - // Add validation middleware if not disabled - if !conf.Server.DevDisableAuthentication { - r.Use(authenticate(api.Users)) - // TODO Validate version - } + // Add validation middleware + r.Use(authenticate(api.Users)) + // TODO Validate version // Subsonic endpoints, grouped by controller r.Group(func(r chi.Router) {