navidrome/server/server.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
1.6 KiB
Go
Raw Normal View History

2020-01-13 23:06:47 +01:00
package server
import (
"net/http"
2020-04-03 23:50:42 +02:00
"path"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2020-01-09 19:51:54 +01:00
"github.com/go-chi/cors"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/conf"
2020-04-03 23:50:42 +02:00
"github.com/navidrome/navidrome/consts"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/ui"
)
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 {
router *chi.Mux
ds model.DataStore
}
func New(ds model.DataStore) *Server {
a := &Server{ds: ds}
initialSetup(ds)
a.initRoutes()
2020-10-06 23:24:16 +02:00
checkFfmpegInstallation()
checkExternalCredentials()
2020-01-11 19:00:03 +01:00
return a
}
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)
a.router.Group(func(r chi.Router) {
r.Use(requestLogger)
2020-04-03 23:50:42 +02:00
r.Mount(urlPath, subRouter)
})
}
func (a *Server) Run(addr string) error {
2020-01-24 01:44:08 +01:00
log.Info("Navidrome server is accepting requests", "address", addr)
return http.ListenAndServe(addr, a.router)
}
2020-01-13 23:06:47 +01:00
func (a *Server) initRoutes() {
r := chi.NewRouter()
r.Use(secureMiddleware())
2020-04-13 16:50:18 +02:00
r.Use(cors.AllowAll().Handler)
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"))
r.Use(middleware.Heartbeat("/ping"))
r.Use(injectLogger)
r.Use(robotsTXT(ui.Assets()))
2020-04-03 23:50:42 +02:00
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html")
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-20 01:34:54 +01:00
a.router = r
}