quintodrome/server/server.go

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

92 lines
2.1 KiB
Go
Raw Normal View History

2020-01-13 13:06:47 -09:00
package server
import (
"net/http"
2020-04-03 13:50:42 -08:00
"path"
"time"
"github.com/deluan/navidrome/assets"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/conf"
2020-04-03 13:50:42 -08:00
"github.com/deluan/navidrome/consts"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/scanner"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2020-01-09 09:51:54 -09:00
"github.com/go-chi/cors"
)
2020-04-03 13:50:42 -08:00
type Handler interface {
http.Handler
Setup(path string)
}
2020-01-13 13:06:47 -09:00
type Server struct {
Scanner *scanner.Scanner
router *chi.Mux
ds model.DataStore
}
func New(scanner *scanner.Scanner, ds model.DataStore) *Server {
a := &Server{Scanner: scanner, ds: ds}
initialSetup(ds)
a.initRoutes()
a.initScanner()
2020-01-11 09:00:03 -09:00
return a
}
2020-04-03 13:50:42 -08: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 13:50:42 -08:00
r.Mount(urlPath, subRouter)
})
}
2020-01-13 13:06:47 -09:00
func (a *Server) Run(addr string) {
2020-01-23 15:44:08 -09:00
log.Info("Navidrome server is accepting requests", "address", addr)
2020-01-08 16:45:07 -09:00
log.Error(http.ListenAndServe(addr, a.router))
}
2020-01-13 13:06:47 -09:00
func (a *Server) initRoutes() {
r := chi.NewRouter()
r.Use(secureMiddleware())
2020-04-13 06:50:18 -08:00
r.Use(cors.AllowAll().Handler)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
2020-01-08 20:18:55 -09:00
r.Use(middleware.Compress(5, "application/xml", "application/json", "application/javascript"))
r.Use(middleware.Heartbeat("/ping"))
r.Use(injectLogger)
r.Use(robotsTXT(assets.AssetFile()))
2020-04-03 13:50:42 -08:00
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html")
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
2020-04-03 13:50:42 -08:00
http.Redirect(w, r, indexHtml, 302)
})
2020-01-19 15:34:54 -09:00
a.router = r
}
2020-01-16 12:53:48 -09:00
func (a *Server) initScanner() {
2020-07-02 12:41:54 -08:00
interval := conf.Server.ScanInterval
2020-03-21 10:20:22 -08:00
if interval == 0 {
log.Warn("Scanner is disabled", "interval", conf.Server.ScanInterval)
return
}
log.Info("Starting scanner", "interval", interval.String())
2020-01-16 12:53:48 -09:00
go func() {
time.Sleep(2 * time.Second)
2020-01-16 12:53:48 -09:00
for {
err := a.Scanner.RescanAll(false)
if err != nil {
2020-01-23 21:29:31 -09:00
log.Error("Error scanning media folder", "folder", conf.Server.MusicFolder, err)
2020-01-16 12:53:48 -09:00
}
time.Sleep(interval)
2020-01-16 12:53:48 -09:00
}
}()
}