Simplify API routes mounting

This commit is contained in:
Deluan 2020-01-11 13:21:43 -05:00 committed by Deluan Quintão
parent 5acc9c7a22
commit 48b465f2fb
2 changed files with 11 additions and 3 deletions

View file

@ -26,17 +26,25 @@ type Router struct {
Scrobbler engine.Scrobbler Scrobbler engine.Scrobbler
Search engine.Search Search engine.Search
MediaFileRepository domain.MediaFileRepository MediaFileRepository domain.MediaFileRepository
mux http.Handler
} }
func NewRouter(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGenerator, func NewRouter(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGenerator,
playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search, playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search,
mediaFileRepository domain.MediaFileRepository) *Router { mediaFileRepository domain.MediaFileRepository) *Router {
return &Router{Browser: browser, Cover: cover, ListGenerator: listGenerator, Playlists: playlists, r := &Router{Browser: browser, Cover: cover, ListGenerator: listGenerator, Playlists: playlists,
Ratings: ratings, Scrobbler: scrobbler, Search: search, MediaFileRepository: mediaFileRepository} Ratings: ratings, Scrobbler: scrobbler, Search: search, MediaFileRepository: mediaFileRepository}
r.mux = r.routes()
return r
} }
func (api *Router) Routes() http.Handler { func (api *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.mux.ServeHTTP(w, r)
}
func (api *Router) routes() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(checkRequiredParameters) r.Use(checkRequiredParameters)

View file

@ -12,6 +12,6 @@ func main() {
fmt.Printf("\nCloudSonic Server v%s\n\n", "0.2") fmt.Printf("\nCloudSonic Server v%s\n\n", "0.2")
a := createApp(conf.Sonic.MusicFolder) a := createApp(conf.Sonic.MusicFolder)
a.MountRouter("/rest/", initRouter().Routes()) a.MountRouter("/rest/", initRouter())
a.Run(":" + conf.Sonic.Port) a.Run(":" + conf.Sonic.Port)
} }