navidrome/server/app/app.go

102 lines
2.4 KiB
Go
Raw Normal View History

2020-01-20 01:34:54 +01:00
package app
import (
2020-01-20 02:40:18 +01:00
"context"
2020-01-20 01:34:54 +01:00
"net/http"
2020-01-20 02:40:18 +01:00
"net/url"
"strings"
2020-01-20 01:34:54 +01:00
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/assets"
"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/model"
2020-01-20 02:40:18 +01:00
"github.com/deluan/rest"
2020-01-20 01:34:54 +01:00
"github.com/go-chi/chi"
2020-01-20 15:54:29 +01:00
"github.com/go-chi/jwtauth"
2020-01-20 01:34:54 +01:00
)
2020-01-20 15:54:29 +01:00
var initialUser = model.User{
UserName: "admin",
Name: "Admin",
IsAdmin: true,
}
2020-01-20 01:34:54 +01:00
type Router struct {
ds model.DataStore
mux http.Handler
path string
}
func New(ds model.DataStore, path string) *Router {
r := &Router{ds: ds, path: path}
r.mux = r.routes()
return r
}
func (app *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.mux.ServeHTTP(w, r)
}
func (app *Router) routes() http.Handler {
r := chi.NewRouter()
2020-01-20 02:40:18 +01:00
// Basic unauthenticated ping
2020-01-20 01:34:54 +01:00
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"response":"pong"}`)) })
2020-01-20 15:54:29 +01:00
r.Post("/login", Login(app.ds))
2020-01-20 02:40:18 +01:00
r.Route("/api", func(r chi.Router) {
if !conf.Sonic.DevDisableAuthentication {
r.Use(jwtauth.Verifier(TokenAuth))
r.Use(Authenticator)
}
app.R(r, "/user", model.User{})
app.R(r, "/song", model.MediaFile{})
app.R(r, "/album", model.Album{})
app.R(r, "/artist", model.Artist{})
2020-01-20 02:40:18 +01:00
})
2020-01-23 00:35:44 +01:00
// Serve UI app assets
r.Handle("/*", http.StripPrefix(app.path, http.FileServer(assets.AssetFile())))
2020-01-20 01:34:54 +01:00
return r
}
2020-01-20 02:40:18 +01:00
func (app *Router) R(r chi.Router, pathPrefix string, model interface{}) {
constructor := func(ctx context.Context) rest.Repository {
return app.ds.Resource(model)
}
2020-01-20 02:40:18 +01:00
r.Route(pathPrefix, func(r chi.Router) {
r.Get("/", rest.GetAll(constructor))
r.Post("/", rest.Post(constructor))
2020-01-20 02:40:18 +01:00
r.Route("/{id:[0-9a-f\\-]+}", func(r chi.Router) {
r.Use(UrlParams)
r.Get("/", rest.Get(constructor))
r.Put("/", rest.Put(constructor))
r.Delete("/", rest.Delete(constructor))
2020-01-20 02:40:18 +01:00
})
})
}
// Middleware to convert Chi URL params (from Context) to query params, as expected by our REST package
func UrlParams(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := chi.RouteContext(r.Context())
parts := make([]string, 0)
for i, key := range ctx.URLParams.Keys {
value := ctx.URLParams.Values[i]
if key == "*" {
continue
}
parts = append(parts, url.QueryEscape(":"+key)+"="+url.QueryEscape(value))
}
q := strings.Join(parts, "&")
if r.URL.RawQuery == "" {
r.URL.RawQuery = q
} else {
r.URL.RawQuery += "&" + q
}
next.ServeHTTP(w, r)
})
}