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