Skip to content

Commit e83598f

Browse files
committed
Modernize codebase
1 parent 8180ee1 commit e83598f

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

auth.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"net"
1313
"net/http"
14+
"slices"
1415
"strings"
1516
)
1617

@@ -20,11 +21,12 @@ const basicAuthScheme string = "Basic "
2021
// custom Key and Secret HTTP headers, or Basic auth from Authorization header.
2122
// Depending on configuration of BasicAuthRealm, KeyHeaderName or SecretHeaderName,
2223
// it can be used as"
23-
// - Basic auth handler - only BasicAuthRealm is set
24-
// - single API key auth handler - only KeyHeaderName is set
25-
// - single API key auth handler with Basic auth support - BasicAuthRealm and KeyHeaderName are set
26-
// - public/secret API key auth handler - KeyHeaderName and SecretHeaderName are set
27-
// - public/secret API key auth handler with Basic auth support - all three are set
24+
// - Basic auth handler - only BasicAuthRealm is set
25+
// - single API key auth handler - only KeyHeaderName is set
26+
// - single API key auth handler with Basic auth support - BasicAuthRealm and KeyHeaderName are set
27+
// - public/secret API key auth handler - KeyHeaderName and SecretHeaderName are set
28+
// - public/secret API key auth handler with Basic auth support - all three are set
29+
//
2830
// By setting AuthorizedNetworks, this handler can authorize requests based only on
2931
// RemoteAddr address.
3032
type AuthHandler[Entity any] struct {
@@ -101,7 +103,7 @@ func getRequestIPs(r *http.Request) (ips []net.IP) {
101103
}
102104
}
103105
if h := r.Header.Get("X-Forwarded-For"); h != "" {
104-
for _, x := range strings.Split(h, ",") {
106+
for x := range strings.SplitSeq(h, ",") {
105107
if i := net.ParseIP(strings.TrimSpace(x)); i != nil {
106108
ips = append(ips, i)
107109
}
@@ -134,11 +136,9 @@ func (h AuthHandler[Entity]) authenticate(r *http.Request) (valid bool, entity E
134136
ips = []net.IP{ip}
135137
}
136138
for _, network := range h.AuthorizedNetworks {
137-
for _, ip := range ips {
138-
if network.Contains(ip) {
139-
valid = true
140-
return
141-
}
139+
if slices.ContainsFunc(ips, network.Contains) {
140+
valid = true
141+
return
142142
}
143143
}
144144
}

file-server/hasher.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/md5"
1010
"encoding/hex"
1111
"io"
12+
"slices"
1213
"strings"
1314
)
1415

@@ -45,13 +46,7 @@ func (s MD5Hasher) IsHash(h string) bool {
4546
}
4647
var found bool
4748
for _, c := range h {
48-
found = false
49-
for _, m := range hexChars {
50-
if c == m {
51-
found = true
52-
break
53-
}
54-
}
49+
found = slices.Contains(hexChars, c)
5550
if !found {
5651
return false
5752
}

server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ func (s *Server) WithHTTP(o HTTPOptions) (err error) {
207207

208208
for domain := range handlers {
209209
var redirectDomain string
210-
if strings.HasPrefix(domain, "www.") {
211-
redirectDomain = strings.TrimPrefix(domain, "www.")
210+
if after, ok := strings.CutPrefix(domain, "www."); ok {
211+
redirectDomain = after
212212
} else {
213213
redirectDomain = "www." + domain
214214
}

templates/functions.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"html/template"
12+
"slices"
1213
"strings"
1314
"time"
1415
)
@@ -76,10 +77,8 @@ func yearRangeFunc(year int) string {
7677
}
7778

7879
func containsStringFunc(list []string, element, yes, no string) string {
79-
for _, e := range list {
80-
if e == element {
81-
return yes
82-
}
80+
if slices.Contains(list, element) {
81+
return yes
8382
}
8483
return no
8584
}

templates/templates.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"html/template"
1212
"log/slog"
13+
"maps"
1314
"net/http"
1415
"os"
1516
"path/filepath"
@@ -101,9 +102,7 @@ func WithTemplateFromFiles(name string, files ...string) Option {
101102
// WithTemplatesFromFiles adds a map of templates parsed from files.
102103
func WithTemplatesFromFiles(ts map[string][]string) Option {
103104
return func(o *Options) {
104-
for name, files := range ts {
105-
o.files[name] = files
106-
}
105+
maps.Copy(o.files, ts)
107106
}
108107
}
109108

@@ -115,9 +114,7 @@ func WithTemplateFromStrings(name string, strings ...string) Option {
115114
// WithTemplatesFromStrings adds a map of templates parsed from strings.
116115
func WithTemplatesFromStrings(ts map[string][]string) Option {
117116
return func(o *Options) {
118-
for name, strings := range ts {
119-
o.strings[name] = strings
120-
}
117+
maps.Copy(o.strings, ts)
121118
}
122119
}
123120

@@ -129,9 +126,7 @@ func WithFunction(name string, fn any) Option {
129126
// WithFunctions adds function map to templates.
130127
func WithFunctions(fns template.FuncMap) Option {
131128
return func(o *Options) {
132-
for name, fn := range fns {
133-
o.functions[name] = fn
134-
}
129+
maps.Copy(o.functions, fns)
135130
}
136131
}
137132

@@ -161,9 +156,7 @@ type Templates struct {
161156
// provided files and strings.
162157
func New(opts ...Option) (t *Templates, err error) {
163158
functions := template.FuncMap{}
164-
for name, fn := range defaultFunctions {
165-
functions[name] = fn
166-
}
159+
maps.Copy(functions, defaultFunctions)
167160
o := &Options{
168161
fileFindFunc: func(f string) string {
169162
return f

0 commit comments

Comments
 (0)