Refactor to use more Go 1.22 features

This commit is contained in:
Deluan 2024-05-12 20:04:11 -04:00
parent 9ee63b39cb
commit 6f2643e55e
3 changed files with 6 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"cmp"
"context" "context"
"errors" "errors"
"fmt" "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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/robots.txt") { if strings.HasSuffix(r.URL.Path, "/robots.txt") {
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 { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
} }
@ -209,7 +210,7 @@ func serverAddressMiddleware(h http.Handler) http.Handler {
h.ServeHTTP(w, r) 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) return http.HandlerFunc(fn)
} }
@ -244,15 +245,15 @@ func serverAddress(r *http.Request) (scheme, host string) {
} }
xfh = xfh[:i] 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 // 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 = cmp.Or(
protocol,
r.Header.Get(xForwardedProto), r.Header.Get(xForwardedProto),
r.Header.Get(xForwardedScheme), r.Header.Get(xForwardedScheme),
r.URL.Scheme, r.URL.Scheme,
protocol,
) )
// If the request host has changed due to the X-Forwarded-Host header, log a trace // If the request host has changed due to the X-Forwarded-Host header, log a trace

View File

@ -13,24 +13,6 @@ func If[T comparable](v T, orElse T) T {
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
}
// P returns a pointer to the input value // P returns a pointer to the input value
func P[T any](v T) *T { func P[T any](v T) *T {
return &v return &v

View File

@ -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() { Describe("P", func() {
It("returns a pointer to the input value", func() { It("returns a pointer to the input value", func() {
v := 123 v := 123