client_ip.go 640 B

123456789101112131415161718192021
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package middleware // import "miniflux.app/middleware"
  5. import (
  6. "context"
  7. "net/http"
  8. "miniflux.app/http/request"
  9. )
  10. // ClientIP stores in the real client IP address in the context.
  11. func (m *Middleware) ClientIP(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. ctx := r.Context()
  14. ctx = context.WithValue(ctx, request.ClientIPContextKey, request.FindClientIP(r))
  15. next.ServeHTTP(w, r.WithContext(ctx))
  16. })
  17. }