2020-01-13 17:06:47 -05:00
|
|
|
package server
|
2020-01-07 14:56:26 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2020-04-03 17:50:42 -04:00
|
|
|
"path"
|
2020-01-07 14:56:26 -05:00
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2020-01-08 09:39:58 -05:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-01-09 13:51:54 -05:00
|
|
|
"github.com/go-chi/cors"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2020-04-03 17:50:42 -04:00
|
|
|
"github.com/navidrome/navidrome/consts"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2021-03-12 11:06:51 -05:00
|
|
|
"github.com/navidrome/navidrome/ui"
|
2020-01-07 14:56:26 -05:00
|
|
|
)
|
|
|
|
|
|
2020-04-03 17:50:42 -04:00
|
|
|
type Handler interface {
|
|
|
|
|
http.Handler
|
|
|
|
|
Setup(path string)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 17:06:47 -05:00
|
|
|
type Server struct {
|
2020-10-24 22:43:59 -04:00
|
|
|
router *chi.Mux
|
|
|
|
|
ds model.DataStore
|
2020-01-07 14:56:26 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-24 22:43:59 -04:00
|
|
|
func New(ds model.DataStore) *Server {
|
|
|
|
|
a := &Server{ds: ds}
|
2020-01-20 15:17:43 -05:00
|
|
|
initialSetup(ds)
|
2020-01-07 14:56:26 -05:00
|
|
|
a.initRoutes()
|
2020-10-06 17:24:16 -04:00
|
|
|
checkFfmpegInstallation()
|
2020-10-21 17:10:06 -04:00
|
|
|
checkExternalCredentials()
|
2020-01-11 13:00:03 -05:00
|
|
|
return a
|
2020-01-07 14:56:26 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-03 17:50:42 -04:00
|
|
|
func (a *Server) MountRouter(urlPath string, subRouter Handler) {
|
|
|
|
|
urlPath = path.Join(conf.Server.BaseURL, urlPath)
|
|
|
|
|
log.Info("Mounting routes", "path", urlPath)
|
|
|
|
|
subRouter.Setup(urlPath)
|
2020-01-07 14:56:26 -05:00
|
|
|
a.router.Group(func(r chi.Router) {
|
2020-04-03 17:50:42 -04:00
|
|
|
r.Mount(urlPath, subRouter)
|
2020-01-07 14:56:26 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 22:43:59 -04:00
|
|
|
func (a *Server) Run(addr string) error {
|
2020-01-23 19:44:08 -05:00
|
|
|
log.Info("Navidrome server is accepting requests", "address", addr)
|
2020-10-24 22:43:59 -04:00
|
|
|
return http.ListenAndServe(addr, a.router)
|
2020-01-07 14:56:26 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-13 17:06:47 -05:00
|
|
|
func (a *Server) initRoutes() {
|
2020-01-07 14:56:26 -05:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
2020-10-04 14:29:33 -04:00
|
|
|
r.Use(secureMiddleware())
|
2020-04-13 10:50:18 -04:00
|
|
|
r.Use(cors.AllowAll().Handler)
|
2020-01-08 09:39:58 -05:00
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
|
r.Use(middleware.RealIP)
|
|
|
|
|
r.Use(middleware.Recoverer)
|
2020-01-09 00:18:55 -05:00
|
|
|
r.Use(middleware.Compress(5, "application/xml", "application/json", "application/javascript"))
|
2020-01-08 09:39:58 -05:00
|
|
|
r.Use(middleware.Heartbeat("/ping"))
|
2020-10-02 10:15:19 -04:00
|
|
|
r.Use(injectLogger)
|
2021-03-25 04:17:36 +01:00
|
|
|
r.Use(requestLogger)
|
2021-03-12 11:06:51 -05:00
|
|
|
r.Use(robotsTXT(ui.Assets()))
|
2020-01-07 14:56:26 -05:00
|
|
|
|
2021-03-29 20:13:04 -04:00
|
|
|
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI)
|
2020-04-03 19:45:35 -04:00
|
|
|
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
2020-04-03 17:50:42 -04:00
|
|
|
http.Redirect(w, r, indexHtml, 302)
|
2020-01-07 14:56:26 -05:00
|
|
|
})
|
2020-01-19 19:34:54 -05:00
|
|
|
|
2020-01-07 14:56:26 -05:00
|
|
|
a.router = r
|
|
|
|
|
}
|