Coverage for utils/get_real_ip.py: 100.00%

6 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-09-03 15:30 +0000

1from fastapi import Request 

2 

3 

4def get_real_ip(request: Request) -> str: 

5 """ 

6 Get the client IP address from the X-Real-IP header set by nginx. 

7 

8 Nginx sets X-Real-IP from $remote_addr (TCP connection IP). 

9 

10 Priority order: 

11 1. X-Real-IP (from nginx $remote_addr) 

12 2. request.client.host (fallback) 

13 """ 

14 real_ip = request.headers.get("x-real-ip") 

15 if real_ip: 

16 return real_ip.strip() 

17 

18 return request.client.host if request.client else "unknown"