mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-06 02:13:29 +03:00
Some refactorings
This commit is contained in:
parent
05d381c26f
commit
b998c05ca0
@ -135,7 +135,7 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler {
|
|||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: true,
|
Secure: true,
|
||||||
SameSite: http.SameSiteStrictMode,
|
SameSite: http.SameSiteStrictMode,
|
||||||
Path: IfZero(conf.Server.BasePath, "/"),
|
Path: If(conf.Server.BasePath, "/"),
|
||||||
}
|
}
|
||||||
http.SetCookie(w, c)
|
http.SetCookie(w, c)
|
||||||
} else {
|
} else {
|
||||||
@ -210,11 +210,11 @@ func serverAddress(r *http.Request) (scheme, host string) {
|
|||||||
}
|
}
|
||||||
xfh = xfh[:i]
|
xfh = xfh[:i]
|
||||||
}
|
}
|
||||||
host = firstOr(r.Host, xfh)
|
host = FirstOr(r.Host, xfh)
|
||||||
|
|
||||||
// Determine the protocol and scheme of the request based on the presence of
|
// Determine the protocol and scheme of the request based on the presence of
|
||||||
// X-Forwarded-* headers or the scheme of the request URL.
|
// X-Forwarded-* headers or the scheme of the request URL.
|
||||||
scheme = firstOr(
|
scheme = FirstOr(
|
||||||
protocol,
|
protocol,
|
||||||
r.Header.Get(xForwardedProto),
|
r.Header.Get(xForwardedProto),
|
||||||
r.Header.Get(xForwardedScheme),
|
r.Header.Get(xForwardedScheme),
|
||||||
@ -231,17 +231,6 @@ func serverAddress(r *http.Request) (scheme, host string) {
|
|||||||
return scheme, host
|
return scheme, host
|
||||||
}
|
}
|
||||||
|
|
||||||
// firstOr is a helper function that returns the first non-empty string from a list
|
|
||||||
// of strings, or a default value if all the strings are empty.
|
|
||||||
func firstOr(or string, strings ...string) string {
|
|
||||||
for _, s := range strings {
|
|
||||||
if s != "" {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return or
|
|
||||||
}
|
|
||||||
|
|
||||||
// URLParamsMiddleware is a middleware function that decodes the query string of
|
// URLParamsMiddleware is a middleware function that decodes the query string of
|
||||||
// the incoming HTTP request, adds the URL parameters from the routing context,
|
// the incoming HTTP request, adds the URL parameters from the routing context,
|
||||||
// and re-encodes the modified query string.
|
// and re-encodes the modified query string.
|
||||||
|
@ -171,7 +171,7 @@ func AbsoluteURL(r *http.Request, u string, params url.Values) string {
|
|||||||
if strings.HasPrefix(u, "/") {
|
if strings.HasPrefix(u, "/") {
|
||||||
buildUrl.Path = path.Join(conf.Server.BasePath, buildUrl.Path)
|
buildUrl.Path = path.Join(conf.Server.BasePath, buildUrl.Path)
|
||||||
if conf.Server.BaseHost != "" {
|
if conf.Server.BaseHost != "" {
|
||||||
buildUrl.Scheme = IfZero(conf.Server.BaseScheme, "http")
|
buildUrl.Scheme = If(conf.Server.BaseScheme, "http")
|
||||||
buildUrl.Host = conf.Server.BaseHost
|
buildUrl.Host = conf.Server.BaseHost
|
||||||
} else {
|
} else {
|
||||||
buildUrl.Scheme = r.URL.Scheme
|
buildUrl.Scheme = r.URL.Scheme
|
||||||
|
@ -166,7 +166,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler {
|
|||||||
MaxAge: consts.CookieExpiry,
|
MaxAge: consts.CookieExpiry,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
SameSite: http.SameSiteStrictMode,
|
SameSite: http.SameSiteStrictMode,
|
||||||
Path: IfZero(conf.Server.BasePath, "/"),
|
Path: If(conf.Server.BasePath, "/"),
|
||||||
}
|
}
|
||||||
http.SetCookie(w, cookie)
|
http.SetCookie(w, cookie)
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,32 @@
|
|||||||
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
|
// Package gg implements simple "extensions" to Go language. Based on https://github.com/icza/gog
|
||||||
package gg
|
package gg
|
||||||
|
|
||||||
// IfZero returns v if it is a non-zero value, orElse otherwise.
|
// If returns v if it is a non-zero value, orElse otherwise.
|
||||||
//
|
//
|
||||||
// This is similar to elvis operator (?:) in Groovy and other languages.
|
// This is similar to elvis operator (?:) in Groovy and other languages.
|
||||||
// Note: Different from the real elvis operator, the orElse expression will always get evaluated.
|
// Note: Different from the real elvis operator, the orElse expression will always get evaluated.
|
||||||
func IfZero[T comparable](v T, orElse T) T {
|
func If[T comparable](v T, orElse T) T {
|
||||||
var zero T
|
var zero T
|
||||||
if v != zero {
|
if v != zero {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return orElse
|
return orElse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FirstOr is a generic helper function that returns the first non-zero value from
|
||||||
|
// a list of comparable values, or a default value if all the values are zero.
|
||||||
|
func FirstOr[T comparable](or T, values ...T) T {
|
||||||
|
// Initialize a zero value of the same type as the input values.
|
||||||
|
var zero T
|
||||||
|
|
||||||
|
// Loop through each input value and check if it is non-zero. If a non-zero value
|
||||||
|
// is found, return it immediately.
|
||||||
|
for _, v := range values {
|
||||||
|
if v != zero {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all the input values are zero, return the default value.
|
||||||
|
return or
|
||||||
|
}
|
||||||
|
@ -15,29 +15,45 @@ func TestGG(t *testing.T) {
|
|||||||
RunSpecs(t, "GG Suite")
|
RunSpecs(t, "GG Suite")
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("IfZero", func() {
|
var _ = Describe("GG", func() {
|
||||||
DescribeTable("string",
|
Describe("If", func() {
|
||||||
func(v, orElse, expected string) {
|
DescribeTable("string",
|
||||||
Expect(gg.IfZero(v, orElse)).To(Equal(expected))
|
func(v, orElse, expected string) {
|
||||||
},
|
Expect(gg.If(v, orElse)).To(Equal(expected))
|
||||||
Entry("zero value", "", "default", "default"),
|
},
|
||||||
Entry("non-zero value", "anything", "default", "anything"),
|
Entry("zero value", "", "default", "default"),
|
||||||
)
|
Entry("non-zero value", "anything", "default", "anything"),
|
||||||
DescribeTable("numeric",
|
)
|
||||||
func(v, orElse, expected int) {
|
DescribeTable("numeric",
|
||||||
Expect(gg.IfZero(v, orElse)).To(Equal(expected))
|
func(v, orElse, expected int) {
|
||||||
},
|
Expect(gg.If(v, orElse)).To(Equal(expected))
|
||||||
Entry("zero value", 0, 2, 2),
|
},
|
||||||
Entry("non-zero value", -1, 2, -1),
|
Entry("zero value", 0, 2, 2),
|
||||||
)
|
Entry("non-zero value", -1, 2, -1),
|
||||||
type testStruct struct {
|
)
|
||||||
field1 int
|
type testStruct struct {
|
||||||
}
|
field1 int
|
||||||
DescribeTable("struct",
|
}
|
||||||
func(v, orElse, expected testStruct) {
|
DescribeTable("struct",
|
||||||
Expect(gg.IfZero(v, orElse)).To(Equal(expected))
|
func(v, orElse, expected testStruct) {
|
||||||
},
|
Expect(gg.If(v, orElse)).To(Equal(expected))
|
||||||
Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}),
|
},
|
||||||
Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}),
|
Entry("zero value", testStruct{}, testStruct{123}, testStruct{123}),
|
||||||
)
|
Entry("non-zero value", testStruct{456}, testStruct{123}, testStruct{456}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("FirstOr", func() {
|
||||||
|
Context("when given a list of strings", func() {
|
||||||
|
It("returns the first non-empty value", func() {
|
||||||
|
Expect(gg.FirstOr("default", "foo", "bar", "baz")).To(Equal("foo"))
|
||||||
|
Expect(gg.FirstOr("default", "", "", "qux")).To(Equal("qux"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("returns the default value if all values are empty", func() {
|
||||||
|
Expect(gg.FirstOr("default", "", "", "")).To(Equal("default"))
|
||||||
|
Expect(gg.FirstOr("", "", "", "")).To(Equal(""))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user