mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 15:05:00 +00:00
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.
20 lines
484 B
Go
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)
|
|
})
|
|
}
|