From d54129ecd2b7205bd358b1416a7a5d994398b408 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sun, 13 Jun 2021 19:15:41 -0400 Subject: [PATCH] Rename `app` package to `nativeapi` --- cmd/root.go | 2 +- cmd/wire_gen.go | 8 +-- cmd/wire_injectors.go | 6 +-- .../{app/app.go => nativeapi/native_api.go} | 46 ++++++++-------- .../native_api_suite_test.go} | 4 +- server/{app => nativeapi}/playlists.go | 2 +- server/{app => nativeapi}/translations.go | 2 +- .../{app => nativeapi}/translations_test.go | 2 +- server/server.go | 54 +++++++++++-------- 9 files changed, 68 insertions(+), 58 deletions(-) rename server/{app/app.go => nativeapi/native_api.go} (68%) rename server/{app/app_suite_test.go => nativeapi/native_api_suite_test.go} (82%) rename server/{app => nativeapi}/playlists.go (99%) rename server/{app => nativeapi}/translations.go (99%) rename server/{app => nativeapi}/translations_test.go (98%) diff --git a/cmd/root.go b/cmd/root.go index 9a165b2c..8e3ba9c5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -74,7 +74,7 @@ func startServer() (func() error, func(err error)) { return func() error { a := CreateServer(conf.Server.MusicFolder) a.MountRouter("Subsonic API", consts.URLPathSubsonicAPI, CreateSubsonicAPIRouter()) - a.MountRouter("Native API", consts.URLPathNativeAPI, CreateAppRouter()) + a.MountRouter("Native API", consts.URLPathNativeAPI, CreateNativeAPIRouter()) return a.Run(fmt.Sprintf("%s:%d", conf.Server.Address, conf.Server.Port)) }, func(err error) { if err != nil { diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 107357ea..6a8d4bc6 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -15,8 +15,8 @@ import ( "github.com/navidrome/navidrome/scanner" "github.com/navidrome/navidrome/scheduler" "github.com/navidrome/navidrome/server" - "github.com/navidrome/navidrome/server/app" "github.com/navidrome/navidrome/server/events" + "github.com/navidrome/navidrome/server/nativeapi" "github.com/navidrome/navidrome/server/subsonic" ) @@ -28,11 +28,11 @@ func CreateServer(musicFolder string) *server.Server { return serverServer } -func CreateAppRouter() *app.Router { +func CreateNativeAPIRouter() *nativeapi.Router { dataStore := persistence.New() broker := GetBroker() share := core.NewShare(dataStore) - router := app.New(dataStore, broker, share) + router := nativeapi.New(dataStore, broker, share) return router } @@ -74,7 +74,7 @@ func createScheduler() scheduler.Scheduler { // wire_injectors.go: -var allProviders = wire.NewSet(core.Set, subsonic.New, app.New, persistence.New, GetBroker) +var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, GetBroker) // Scanner must be a Singleton var ( diff --git a/cmd/wire_injectors.go b/cmd/wire_injectors.go index 4851ee61..352ed268 100644 --- a/cmd/wire_injectors.go +++ b/cmd/wire_injectors.go @@ -11,15 +11,15 @@ import ( "github.com/navidrome/navidrome/scanner" "github.com/navidrome/navidrome/scheduler" "github.com/navidrome/navidrome/server" - "github.com/navidrome/navidrome/server/app" "github.com/navidrome/navidrome/server/events" + "github.com/navidrome/navidrome/server/nativeapi" "github.com/navidrome/navidrome/server/subsonic" ) var allProviders = wire.NewSet( core.Set, subsonic.New, - app.New, + nativeapi.New, persistence.New, GetBroker, ) @@ -31,7 +31,7 @@ func CreateServer(musicFolder string) *server.Server { )) } -func CreateAppRouter() *app.Router { +func CreateNativeAPIRouter() *nativeapi.Router { panic(wire.Build( allProviders, )) diff --git a/server/app/app.go b/server/nativeapi/native_api.go similarity index 68% rename from server/app/app.go rename to server/nativeapi/native_api.go index 419b7a3d..7c2ffc3a 100644 --- a/server/app/app.go +++ b/server/nativeapi/native_api.go @@ -1,4 +1,4 @@ -package app +package nativeapi import ( "context" @@ -28,22 +28,22 @@ func New(ds model.DataStore, broker events.Broker, share core.Share) *Router { return r } -func (app *Router) routes() http.Handler { +func (n *Router) routes() http.Handler { r := chi.NewRouter() - r.Use(server.Authenticator(app.ds)) + r.Use(server.Authenticator(n.ds)) r.Use(server.JWTRefresher) - app.R(r, "/user", model.User{}, true) - app.R(r, "/song", model.MediaFile{}, true) - app.R(r, "/album", model.Album{}, true) - app.R(r, "/artist", model.Artist{}, true) - app.R(r, "/player", model.Player{}, true) - app.R(r, "/playlist", model.Playlist{}, true) - app.R(r, "/transcoding", model.Transcoding{}, conf.Server.EnableTranscodingConfig) - app.RX(r, "/share", app.share.NewRepository, true) - app.RX(r, "/translation", newTranslationRepository, false) + n.R(r, "/user", model.User{}, true) + n.R(r, "/song", model.MediaFile{}, true) + n.R(r, "/album", model.Album{}, true) + n.R(r, "/artist", model.Artist{}, true) + n.R(r, "/player", model.Player{}, true) + n.R(r, "/playlist", model.Playlist{}, true) + n.R(r, "/transcoding", model.Transcoding{}, conf.Server.EnableTranscodingConfig) + n.RX(r, "/share", n.share.NewRepository, true) + n.RX(r, "/translation", newTranslationRepository, false) - app.addPlaylistTrackRoute(r) + n.addPlaylistTrackRoute(r) // Keepalive endpoint to be used to keep the session valid (ex: while playing songs) r.Get("/keepalive/*", func(w http.ResponseWriter, r *http.Request) { @@ -51,20 +51,20 @@ func (app *Router) routes() http.Handler { }) if conf.Server.DevActivityPanel { - r.Handle("/events", app.broker) + r.Handle("/events", n.broker) } return r } -func (app *Router) R(r chi.Router, pathPrefix string, model interface{}, persistable bool) { +func (n *Router) R(r chi.Router, pathPrefix string, model interface{}, persistable bool) { constructor := func(ctx context.Context) rest.Repository { - return app.ds.Resource(ctx, model) + return n.ds.Resource(ctx, model) } - app.RX(r, pathPrefix, constructor, persistable) + n.RX(r, pathPrefix, constructor, persistable) } -func (app *Router) RX(r chi.Router, pathPrefix string, constructor rest.RepositoryConstructor, persistable bool) { +func (n *Router) RX(r chi.Router, pathPrefix string, constructor rest.RepositoryConstructor, persistable bool) { r.Route(pathPrefix, func(r chi.Router) { r.Get("/", rest.GetAll(constructor)) if persistable { @@ -81,22 +81,22 @@ func (app *Router) RX(r chi.Router, pathPrefix string, constructor rest.Reposito }) } -func (app *Router) addPlaylistTrackRoute(r chi.Router) { +func (n *Router) addPlaylistTrackRoute(r chi.Router) { r.Route("/playlist/{playlistId}/tracks", func(r chi.Router) { r.Get("/", func(w http.ResponseWriter, r *http.Request) { - getPlaylist(app.ds)(w, r) + getPlaylist(n.ds)(w, r) }) r.Route("/{id}", func(r chi.Router) { r.Use(urlParams) r.Put("/", func(w http.ResponseWriter, r *http.Request) { - reorderItem(app.ds)(w, r) + reorderItem(n.ds)(w, r) }) r.Delete("/", func(w http.ResponseWriter, r *http.Request) { - deleteFromPlaylist(app.ds)(w, r) + deleteFromPlaylist(n.ds)(w, r) }) }) r.With(urlParams).Post("/", func(w http.ResponseWriter, r *http.Request) { - addToPlaylist(app.ds)(w, r) + addToPlaylist(n.ds)(w, r) }) }) } diff --git a/server/app/app_suite_test.go b/server/nativeapi/native_api_suite_test.go similarity index 82% rename from server/app/app_suite_test.go rename to server/nativeapi/native_api_suite_test.go index d940aef3..d06af718 100644 --- a/server/app/app_suite_test.go +++ b/server/nativeapi/native_api_suite_test.go @@ -1,4 +1,4 @@ -package app +package nativeapi import ( "testing" @@ -13,5 +13,5 @@ func TestNativeApi(t *testing.T) { tests.Init(t, false) log.SetLevel(log.LevelCritical) RegisterFailHandler(Fail) - RunSpecs(t, "RESTful API Suite") + RunSpecs(t, "Native RESTful API Suite") } diff --git a/server/app/playlists.go b/server/nativeapi/playlists.go similarity index 99% rename from server/app/playlists.go rename to server/nativeapi/playlists.go index 01c71039..3748ca90 100644 --- a/server/app/playlists.go +++ b/server/nativeapi/playlists.go @@ -1,4 +1,4 @@ -package app +package nativeapi import ( "context" diff --git a/server/app/translations.go b/server/nativeapi/translations.go similarity index 99% rename from server/app/translations.go rename to server/nativeapi/translations.go index 17aa76bb..fcd82460 100644 --- a/server/app/translations.go +++ b/server/nativeapi/translations.go @@ -1,4 +1,4 @@ -package app +package nativeapi import ( "bytes" diff --git a/server/app/translations_test.go b/server/nativeapi/translations_test.go similarity index 98% rename from server/app/translations_test.go rename to server/nativeapi/translations_test.go index 63bdb8b1..732d312f 100644 --- a/server/app/translations_test.go +++ b/server/nativeapi/translations_test.go @@ -1,4 +1,4 @@ -package app +package nativeapi import ( "encoding/json" diff --git a/server/server.go b/server/server.go index 4c4d1647..c1686460 100644 --- a/server/server.go +++ b/server/server.go @@ -18,34 +18,38 @@ import ( ) type Server struct { - router *chi.Mux - ds model.DataStore + router *chi.Mux + ds model.DataStore + appRoot string } func New(ds model.DataStore) *Server { - a := &Server{ds: ds} + s := &Server{ds: ds} initialSetup(ds) - a.initRoutes() + s.initRoutes() checkFfmpegInstallation() checkExternalCredentials() - return a + return s } -func (a *Server) MountRouter(description, urlPath string, subRouter http.Handler) { +func (s *Server) MountRouter(description, urlPath string, subRouter http.Handler) { urlPath = path.Join(conf.Server.BaseURL, urlPath) log.Info(fmt.Sprintf("Mounting %s routes", description), "path", urlPath) - a.router.Group(func(r chi.Router) { + s.router.Group(func(r chi.Router) { r.Mount(urlPath, subRouter) }) } -func (a *Server) Run(addr string) error { +func (s *Server) Run(addr string) error { + s.MountRouter("WebUI", consts.URLPathUI, s.frontendAssetsHandler()) log.Info("Navidrome server is accepting requests", "address", addr) - return http.ListenAndServe(addr, a.router) + return http.ListenAndServe(addr, s.router) } -func (a *Server) initRoutes() { - auth.Init(a.ds) +func (s *Server) initRoutes() { + auth.Init(s.ds) + + s.appRoot = path.Join(conf.Server.BaseURL, consts.URLPathUI) r := chi.NewRouter() @@ -68,25 +72,31 @@ func (a *Server) initRoutes() { "windowLength", conf.Server.AuthWindowLength) rateLimiter := httprate.LimitByIP(conf.Server.AuthRequestLimit, conf.Server.AuthWindowLength) - r.With(rateLimiter).Post("/login", login(a.ds)) + r.With(rateLimiter).Post("/login", login(s.ds)) } else { log.Warn("Login rate limit is disabled! Consider enabling it to be protected against brute-force attacks") - r.Post("/login", login(a.ds)) + r.Post("/login", login(s.ds)) } - r.Post("/createAdmin", createAdmin(a.ds)) + r.Post("/createAdmin", createAdmin(s.ds)) }) - // Serve UI app assets - appRoot := path.Join(conf.Server.BaseURL, consts.URLPathUI) + // Redirect root to UI URL r.Get("/*", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, appRoot+"/", 302) + http.Redirect(w, r, s.appRoot+"/", http.StatusFound) }) - r.Get(appRoot, func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, appRoot+"/", 302) + r.Get(s.appRoot, func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, s.appRoot+"/", http.StatusFound) }) - r.Handle(appRoot+"/", serveIndex(a.ds, ui.Assets())) - r.Handle(appRoot+"/*", http.StripPrefix(appRoot, http.FileServer(http.FS(ui.Assets())))) - a.router = r + s.router = r +} + +// Serve UI app assets +func (s *Server) frontendAssetsHandler() http.Handler { + r := chi.NewRouter() + + r.Handle("/", serveIndex(s.ds, ui.Assets())) + r.Handle("/*", http.StripPrefix(s.appRoot, http.FileServer(http.FS(ui.Assets())))) + return r }