navidrome/server/server.go

107 lines
2.7 KiB
Go
Raw Normal View History

2020-01-13 23:06:47 +01:00
package server
import (
"net/http"
"os"
2020-04-03 23:50:42 +02:00
"path"
"path/filepath"
"time"
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"
"github.com/deluan/navidrome/scanner"
"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-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 {
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 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) {
2020-02-02 02:07:15 +01:00
r.Use(RequestLogger)
2020-04-03 23:50:42 +02:00
r.Mount(urlPath, subRouter)
})
}
2020-01-13 23:06:47 +01:00
func (a *Server) Run(addr string) {
2020-01-24 01:44:08 +01:00
log.Info("Navidrome server is accepting requests", "address", addr)
2020-01-09 02:45:07 +01:00
log.Error(http.ListenAndServe(addr, a.router))
}
2020-01-13 23:06:47 +01:00
func (a *Server) initRoutes() {
r := chi.NewRouter()
2020-01-09 19:51:54 +01:00
r.Use(cors.Default().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"))
2020-01-09 02:45:07 +01:00
r.Use(InjectLogger)
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
workDir, _ := os.Getwd()
2020-01-08 17:13:05 +01:00
filesDir := filepath.Join(workDir, "Jamstash-master/dist")
2020-01-20 01:34:54 +01:00
FileServer(r, "/Jamstash", "/Jamstash", http.Dir(filesDir))
a.router = r
}
2020-01-16 22:53:48 +01:00
func (a *Server) initScanner() {
2020-01-24 07:29:31 +01:00
interval, err := time.ParseDuration(conf.Server.ScanInterval)
if err != nil {
log.Error("Invalid interval specification. Using default of 5m", "interval", conf.Server.ScanInterval, err)
interval = 5 * time.Minute
}
2020-03-21 19:20:22 +01:00
if interval == 0 {
log.Warn("Scanner is disabled", "interval", conf.Server.ScanInterval)
return
}
log.Info("Starting scanner", "interval", interval.String())
2020-01-16 22:53:48 +01:00
go func() {
time.Sleep(2 * time.Second)
2020-01-16 22:53:48 +01:00
for {
err := a.Scanner.RescanAll(false)
if err != nil {
2020-01-24 07:29:31 +01:00
log.Error("Error scanning media folder", "folder", conf.Server.MusicFolder, err)
2020-01-16 22:53:48 +01:00
}
time.Sleep(interval)
2020-01-16 22:53:48 +01:00
}
}()
}
2020-01-09 02:45:07 +01:00
func InjectLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = log.NewContext(r.Context(), "requestId", ctx.Value(middleware.RequestIDKey))
next.ServeHTTP(w, r.WithContext(ctx))
})
}