C.W.K.
Stream
Lesson 06 of 06 · published

Production 패턴 — Middleware, CORS, 에러, 로그

~12 min · middleware, cors, errors

Level 0호기심
0 XP0/65 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

CORS — 잠겨있지 열려있지 않음

cwkPippa 의 CORS 가 세 origin 허용: localhost:5173, 127.0.0.1:5173, Tailscale IP 100.x.y.z:5173. 그 외 다 reject. Tailnet 의 새 device? IP 추가. default 'allow all origins' 는 보안 구멍, 사적 box 에서도.

Middleware — 요청 lifecycle hook

FastAPI middleware 는 모든 요청 wrap. cwkPippa 는 적게 사용: request-id 생성기, 구조화된 logger, heartbeat 와 coordinate 하는 streaming guard (Heart track, lesson 4). middleware 는 cheap 해야 — 비싼 건 route 또는 background task 에.

에러 — 시끄럽게 fail, 정직하게 fail

silent catch X. adapter raise 하면, route 가 SSE 위 error event 띄워서 frontend 가 'I got stuck — try again' 보여줘. chat 이 'fine 해 보이도록' 예외 삼키지 마. 다른 각도에서 broken-chat 죄.

로그 — string 아닌 structured

각 log line 이 JSON: timestamp, level, request_id, message, plus structured fields. grep 쉽고, jq 로 parse 쉽고, log viewer 에 feed 쉽고. plain-text log 는 일정 양 넘으면 unreadable.

Code

CORS — locked allowlist·python
from fastapi.middleware.cors import CORSMiddleware

_allowed_origins = [
    'http://localhost:5173',
    'http://127.0.0.1:5173',
    'http://100.x.y.z:5173',  # Tailscale
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=_allowed_origins,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고
💛 by 똘이warm💛 by 피파warm

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.