23 lines
450 B
Go
23 lines
450 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"hk/pkg/middleware"
|
|
"net/http"
|
|
)
|
|
|
|
type RealIPMiddleware struct {
|
|
}
|
|
|
|
func NewRealIPMiddleware() *RealIPMiddleware {
|
|
return &RealIPMiddleware{}
|
|
}
|
|
|
|
func (m *RealIPMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
ipaddress := middleware.GetRealIP(r)
|
|
ctx := context.WithValue(r.Context(), "ip", ipaddress)
|
|
next(w, r.WithContext(ctx))
|
|
}
|
|
}
|