Skip to content

Commit c269b46

Browse files
committed
🔨 fix: ip logging (related: #14)
1 parent a1e0486 commit c269b46

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ cython_debug/
173173
# Credentials
174174
local.env
175175
prod.env
176+
local.yaml
176177

177178
# Etc
178179
junit.xml

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ test:
1717

1818
.PHONY: local
1919
local:
20-
export DESCRIPTION=$$(cat README.md) && source ./envs/local.env && uv run uvicorn app.main:app --host 0.0.0.0 --port $${PORT} --env-file ./envs/local.env --reload --log-level debug
20+
export DESCRIPTION=$$(cat README.md) && source ./envs/local.env && uv run uvicorn app.main:app --host 0.0.0.0 --port $${PORT} --env-file ./envs/local.env --proxy-headers --forwarded-allow-ips='*' --reload
2121

2222
.PHONY: prod
2323
prod:
24-
export DESCRIPTION=$$(cat README.md) && uv run uvicorn app.main:app --host 0.0.0.0 --env-file ./envs/prod.env
24+
export DESCRIPTION=$$(cat README.md) && uv run uvicorn app.main:app --host 0.0.0.0 --env-file ./envs/prod.env --proxy-headers --forwarded-allow-ips='*'

app/core/middlewares.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ class LoggingMiddleware(BaseHTTPMiddleware):
1111
async def dispatch(
1212
self, request: Request, call_next: RequestResponseEndpoint
1313
) -> Response:
14-
if request.client:
15-
ip = ansi_format(
16-
f"{request.client.host}:{request.client.port}",
17-
bg_color=ANSI_BG_COLOR.LIGHT_BLACK,
18-
style=[ANSI_STYLE.UNDERLINE, ANSI_STYLE.BOLD],
19-
)
14+
if request.headers.get("x-real-ip"):
15+
ip = request.headers.get("x-real-ip")
16+
logger.info(ip)
17+
elif request.headers.get("x-forwarded-for"):
18+
ip = request.headers.get("x-forwarded-for")
19+
logger.info(ip)
20+
elif request.client:
21+
ip = request.client.host
22+
logger.info(ip)
2023
else:
2124
ip = "None"
25+
ip = ansi_format(
26+
f"{ip}",
27+
bg_color=ANSI_BG_COLOR.LIGHT_BLACK,
28+
style=[ANSI_STYLE.UNDERLINE, ANSI_STYLE.BOLD],
29+
)
2230
url = ansi_format(
2331
str(request.url),
2432
bg_color=ANSI_BG_COLOR.LIGHT_BLACK,

0 commit comments

Comments
 (0)