Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ linters:
- unconvert
- unparam
- unused
- modernize
- testifylint
settings:
goconst:
min-len: 2
Expand Down
5 changes: 1 addition & 4 deletions benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ func (b *Benchmarks) Stats(interval time.Duration) BenchmarkStats {
}

// ensure we calculate rate based on actual interval
actualInterval := fnInterval.Sub(stInterval)
if actualInterval < time.Second {
actualInterval = time.Second
}
actualInterval := max(fnInterval.Sub(stInterval), time.Second)

return BenchmarkStats{
Requests: requests,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.24.0

require (
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.45.0
golang.org/x/crypto v0.46.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/sys v0.39.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var gzDefaultContentTypes = []string{
}

var gzPool = sync.Pool{
New: func() interface{} { return gzip.NewWriter(io.Discard) },
New: func() any { return gzip.NewWriter(io.Discard) },
}

type gzipResponseWriter struct {
Expand Down
4 changes: 2 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Middleware struct {

// Backend is logging backend
type Backend interface {
Logf(format string, args ...interface{})
Logf(format string, args ...any)
}

type logParts struct {
Expand All @@ -51,7 +51,7 @@ type logParts struct {

type stdBackend struct{}

func (s stdBackend) Logf(format string, args ...interface{}) {
func (s stdBackend) Logf(format string, args ...any) {
log.Printf(format, args...)
}

Expand Down
11 changes: 7 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ func AppInfo(app, author, version string) func(http.Handler) http.Handler {
return f
}

// Ping middleware response with pong to /ping. Stops chain if ping request detected
// Ping middleware response with pong to /ping. Stops chain if ping request detected.
// Handles both GET and HEAD methods - HEAD returns headers only without body,
// which is useful for lightweight health checks by monitoring tools.
func Ping(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {

if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/ping") {
if (r.Method == "GET" || r.Method == "HEAD") && strings.HasSuffix(strings.ToLower(r.URL.Path), "/ping") {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong"))
if r.Method == "GET" {
_, _ = w.Write([]byte("pong"))
}
return
}
next.ServeHTTP(w, r)
Expand Down
59 changes: 44 additions & 15 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,58 @@ func TestMiddleware_AppInfo(t *testing.T) {
}

func TestMiddleware_Ping(t *testing.T) {

handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(Ping(handler))
defer ts.Close()

resp, err := http.Get(ts.URL + "/ping")
require.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "pong", string(b))
t.Run("GET returns pong", func(t *testing.T) {
resp, err := http.Get(ts.URL + "/ping")
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "pong", string(b))
})

resp, err = http.Get(ts.URL + "/blah")
require.Nil(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "blah blah", string(b))
t.Run("HEAD returns 200 with no body", func(t *testing.T) {
req, err := http.NewRequest(http.MethodHead, ts.URL+"/ping", http.NoBody)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Empty(t, b, "HEAD should return empty body")
})

t.Run("POST passes to next handler", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, ts.URL+"/ping", http.NoBody)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "blah blah", string(b))
})

t.Run("other paths pass to next handler", func(t *testing.T) {
resp, err := http.Get(ts.URL + "/blah")
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "blah blah", string(b))
})
}

func TestMiddleware_Recoverer(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions realip/real.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func Get(r *http.Request) (string, error) {

// check X-Forwarded-For, find leftmost public IP
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
addresses := strings.Split(xff, ",")
for _, addr := range addresses {
for addr := range strings.SplitSeq(xff, ",") {
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function strings.SplitSeq does not exist in the Go standard library. The standard function is strings.Split which returns a slice of strings. If you intended to use an iterator-based approach available in Go 1.23+, you would need strings.Fields or similar, but strings.SplitSeq is not a valid function name. This change will cause a compilation error.

Suggested change
for addr := range strings.SplitSeq(xff, ",") {
for _, addr := range strings.Split(xff, ",") {

Copilot uses AI. Check for mistakes.
ip := strings.TrimSpace(addr)
if parsedIP := net.ParseIP(ip); isPublicIP(parsedIP) {
return ip, nil
Expand Down
9 changes: 4 additions & 5 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type JSON map[string]any

// RenderJSON sends data as json
func RenderJSON(w http.ResponseWriter, data interface{}) {
func RenderJSON(w http.ResponseWriter, data any) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
Expand All @@ -35,9 +35,8 @@ func RenderJSONFromBytes(w http.ResponseWriter, r *http.Request, data []byte) er
}

// RenderJSONWithHTML allows html tags and forces charset=utf-8
func RenderJSONWithHTML(w http.ResponseWriter, r *http.Request, v interface{}) error {

encodeJSONWithHTML := func(v interface{}) ([]byte, error) {
func RenderJSONWithHTML(w http.ResponseWriter, r *http.Request, v any) error {
encodeJSONWithHTML := func(v any) ([]byte, error) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
Expand All @@ -55,7 +54,7 @@ func RenderJSONWithHTML(w http.ResponseWriter, r *http.Request, v interface{}) e
}

// renderJSONWithStatus sends data as json and enforces status code
func renderJSONWithStatus(w http.ResponseWriter, data interface{}, code int) {
func renderJSONWithStatus(w http.ResponseWriter, data any, code int) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
Expand Down