golib/net/ipcohort/middleware.go
AJ ONeal 051a244083
fix(ipcohort): never return nil Cohort, zero value is empty set
All constructors (LoadFile, LoadFiles, ReadAll) now return &Cohort{}
on error instead of nil. Callers can use the result directly without
nil checks — an empty Cohort is a valid empty set that returns false
for all Contains calls.
2026-05-04 14:28:53 -06:00

20 lines
484 B
Go

package ipcohort
import (
"net/http"
"strings"
)
// HTTP middleware example
func (c *Cohort) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get client IP (basic; improve with X-Forwarded-For parsing if behind proxy)
ip, _, _ := strings.Cut(r.RemoteAddr, ":")
if blocked, _ := c.Contains(ip); blocked {
http.Error(w, "Access denied", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}