Rename `app` package to `nativeapi`

This commit is contained in:
Deluan 2021-06-13 19:15:41 -04:00
parent 03efc48137
commit d54129ecd2
9 changed files with 68 additions and 58 deletions

View File

@ -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 {

View File

@ -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 (

View File

@ -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,
))

View File

@ -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)
})
})
}

View File

@ -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")
}

View File

@ -1,4 +1,4 @@
package app
package nativeapi
import (
"context"

View File

@ -1,4 +1,4 @@
package app
package nativeapi
import (
"bytes"

View File

@ -1,4 +1,4 @@
package app
package nativeapi
import (
"encoding/json"

View File

@ -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
}