Skip to content

🎧 Your Personal Streaming Service

Refactor to use more Go 1.22 features

Deluan6f2643e55e93
diff --git a/server/middlewares.go b/server/middlewares.go
index dd4d62660..745f34577 100644
--- a/server/middlewares.go
+++ b/server/middlewares.go
@@ -1,6 +1,7 @@
package server
import (
+ "cmp"
"context"
"errors"
"fmt"
@@ -71,7 +72,7 @@ func robotsTXT(fs fs.FS) func(http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/robots.txt") {
r.URL.Path = "/robots.txt"
- http.FileServer(http.FS(fs)).ServeHTTP(w, r)
+ http.FileServerFS(fs).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
@@ -209,7 +210,7 @@ func serverAddressMiddleware(h http.Handler) http.Handler {
h.ServeHTTP(w, r)
}
- // Return the new handler function as an http.Handler object.
+ // Return the new handler function as a http.Handler object.
return http.HandlerFunc(fn)
}
@@ -244,15 +245,15 @@ func serverAddress(r *http.Request) (scheme, host string) {
}
xfh = xfh[:i]
}
- host = FirstOr(r.Host, xfh)
+ host = cmp.Or(xfh, r.Host)
// Determine the protocol and scheme of the request based on the presence of
// X-Forwarded-* headers or the scheme of the request URL.
- scheme = FirstOr(
- protocol,
+ scheme = cmp.Or(
r.Header.Get(xForwardedProto),
r.Header.Get(xForwardedScheme),
r.URL.Scheme,
+ protocol,
)
// If the request host has changed due to the X-Forwarded-Host header, log a trace
diff --git a/utils/gg/gg.go b/utils/gg/gg.go
index 8a046a2cd..2b9ebb9e6 100644
--- a/utils/gg/gg.go
+++ b/utils/gg/gg.go
@@ -13,24 +13,6 @@ func If[T comparable](v T, orElse T) T {
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
-}
-
// P returns a pointer to the input value
func P[T any](v T) *T {
return &v
diff --git a/utils/gg/gg_test.go b/utils/gg/gg_test.go
index 3622522d6..94c997eb0 100644
--- a/utils/gg/gg_test.go
+++ b/utils/gg/gg_test.go
@@ -43,20 +43,6 @@ var _ = Describe("GG", func() {
)
})
- 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(""))
- })
- })
- })
-
Describe("P", func() {
It("returns a pointer to the input value", func() {
v := 123