Some refactorings

This commit is contained in:
Deluan
2023-03-26 21:26:55 -04:00
parent 05d381c26f
commit b998c05ca0
5 changed files with 66 additions and 43 deletions
+3 -14
View File
@@ -135,7 +135,7 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler {
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
Path: IfZero(conf.Server.BasePath, "/"),
Path: If(conf.Server.BasePath, "/"),
}
http.SetCookie(w, c)
} else {
@@ -210,11 +210,11 @@ func serverAddress(r *http.Request) (scheme, host string) {
}
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
// X-Forwarded-* headers or the scheme of the request URL.
scheme = firstOr(
scheme = FirstOr(
protocol,
r.Header.Get(xForwardedProto),
r.Header.Get(xForwardedScheme),
@@ -231,17 +231,6 @@ func serverAddress(r *http.Request) (scheme, host string) {
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
// the incoming HTTP request, adds the URL parameters from the routing context,
// and re-encodes the modified query string.
+1 -1
View File
@@ -171,7 +171,7 @@ func AbsoluteURL(r *http.Request, u string, params url.Values) string {
if strings.HasPrefix(u, "/") {
buildUrl.Path = path.Join(conf.Server.BasePath, buildUrl.Path)
if conf.Server.BaseHost != "" {
buildUrl.Scheme = IfZero(conf.Server.BaseScheme, "http")
buildUrl.Scheme = If(conf.Server.BaseScheme, "http")
buildUrl.Host = conf.Server.BaseHost
} else {
buildUrl.Scheme = r.URL.Scheme
+1 -1
View File
@@ -166,7 +166,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler {
MaxAge: consts.CookieExpiry,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Path: IfZero(conf.Server.BasePath, "/"),
Path: If(conf.Server.BasePath, "/"),
}
http.SetCookie(w, cookie)
}