navidrome/app.go

101 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
2020-01-09 02:45:07 +01:00
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/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"
)
type App struct {
2020-01-11 19:00:03 +01:00
Importer *scanner.Importer
router *chi.Mux
}
2020-01-11 19:00:03 +01:00
func NewApp(importer *scanner.Importer) *App {
a := &App{Importer: importer}
2020-01-08 16:25:23 +01:00
initMimeTypes()
a.initRoutes()
a.initImporter()
2020-01-11 19:00:03 +01:00
return a
}
func (a *App) MountRouter(path string, subRouter http.Handler) {
a.router.Group(func(r chi.Router) {
r.Use(middleware.Logger)
r.Mount(path, subRouter)
})
}
func (a *App) Run(addr string) {
2020-01-09 02:45:07 +01:00
log.Info("Started CloudSonic server", "address", addr)
log.Error(http.ListenAndServe(addr, a.router))
}
func (a *App) 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)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
2020-01-08 17:13:05 +01:00
http.Redirect(w, r, "/Jamstash", 302)
})
workDir, _ := os.Getwd()
2020-01-08 17:13:05 +01:00
filesDir := filepath.Join(workDir, "Jamstash-master/dist")
FileServer(r, "/Jamstash", http.Dir(filesDir))
a.router = r
}
func (a *App) initImporter() {
go a.startPeriodicScans()
}
func (a *App) startPeriodicScans() {
for {
select {
case <-time.After(5 * time.Second):
2020-01-11 19:00:03 +01:00
a.Importer.CheckForUpdates(false)
}
}
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
2020-01-11 22:56:08 +01:00
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
2020-01-11 22:56:08 +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))
})
}