Browse Source

๐ŸŒ Add Korean translation for `docs/ko/docs/advanced/middlewares.md` (#12753)

pull/12996/head
nahyunkeem 4 months ago
committed by GitHub
parent
commit
4eefb2616a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 96
      docs/ko/docs/advanced/middlewares.md

96
docs/ko/docs/advanced/middlewares.md

@ -0,0 +1,96 @@
# ๊ณ ๊ธ‰ ๋ฏธ๋“ค์›จ์–ด
๋ฉ”์ธ ํŠœํ† ๋ฆฌ์–ผ์—์„œ [Custom Middleware](../tutorial/middleware.md){.internal-link target=_blank}๋ฅผ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฝ์œผ์…จ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  [CORS with the `CORSMiddleware`](){.internal-link target=_blank}ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋ณด์…จ์Šต๋‹ˆ๋‹ค.
์ด ์„น์…˜์—์„œ๋Š” ๋‹ค๋ฅธ ๋ฏธ๋“ค์›จ์–ด๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
## ASGI ๋ฏธ๋“ค์›จ์–ด ์ถ”๊ฐ€ํ•˜๊ธฐ
**FastAPI**๋Š” Starlette์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, <abbr title="Asynchronous Server Gateway Interface">ASGI</abbr> ์‚ฌ์–‘์„ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ ASGI ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฏธ๋“ค์›จ์–ด๊ฐ€ FastAPI๋‚˜ Starlette์šฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•„๋„ ASGI ์‚ฌ์–‘์„ ์ค€์ˆ˜ํ•˜๋Š” ํ•œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ASGI ๋ฏธ๋“ค์›จ์–ด๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ASGI ์•ฑ์„ ๋ฐ›๋Š” ํด๋ž˜์Šค๋“ค์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ํƒ€์‚ฌ ASGI ๋ฏธ๋“ค์›จ์–ด ๋ฌธ์„œ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋„๋ก ์•ˆ๋‚ดํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
```Python
from unicorn import UnicornMiddleware
app = SomeASGIApp()
new_app = UnicornMiddleware(app, some_config="rainbow")
```
ํ•˜์ง€๋งŒ ๋‚ด๋ถ€ ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋Š” ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” FastAPI(์‹ค์ œ๋กœ๋Š” Starlette)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ์œ„ํ•ด `app.add_middleware()`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(CORS์˜ ์˜ˆ์—์„œ์™€ ๊ฐ™์ด).
```Python
from fastapi import FastAPI
from unicorn import UnicornMiddleware
app = FastAPI()
app.add_middleware(UnicornMiddleware, some_config="rainbow")
```
`app.add_middleware()`๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋ฏธ๋“ค์›จ์–ด ํด๋ž˜์Šค์™€ ๋ฏธ๋“ค์›จ์–ด์— ์ „๋‹ฌํ•  ์ถ”๊ฐ€ ์ธ์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.
## ํ†ตํ•ฉ ๋ฏธ๋“ค์›จ์–ด
**FastAPI**์—๋Š” ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ์—์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
/// note | ๊ธฐ์ˆ  ์„ธ๋ถ€ ์‚ฌํ•ญ
๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” `from starlette.middleware.something import SomethingMiddleware`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
**FastAPI**๋Š” ๊ฐœ๋ฐœ์ž์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด `fastapi.middleware`์— ์—ฌ๋Ÿฌ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๋ฏธ๋“ค์›จ์–ด๋Š” Starlette์—์„œ ์ง์ ‘ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
///
## `HTTPSRedirectMiddleware`
๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์ด `https` ๋˜๋Š” `wss`์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
`http` ๋˜๋Š” `ws`๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์€ ๋Œ€์‹  ๋ณด์•ˆ ์ฒด๊ณ„๋กœ ๋ฆฌ๋””๋ ‰์…˜๋ฉ๋‹ˆ๋‹ค.
{* ../../docs_src/advanced_middleware/tutorial001.py hl[2,6] *}
## `TrustedHostMiddleware`
HTTP ํ˜ธ์ŠคํŠธ ํ—ค๋” ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ˆ˜์‹  ์š”์ฒญ์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •๋œ `Host` ํ—ค๋”๋ฅผ ๊ฐ–๋„๋ก ๊ฐ•์ œํ•ฉ๋‹ˆ๋‹ค.
{* ../../docs_src/advanced_middleware/tutorial002.py hl[2,6:8] *}
๋‹ค์Œ ์ธ์ˆ˜๊ฐ€ ์ง€์›๋ฉ๋‹ˆ๋‹ค:
* `allowed_hosts` - ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์œผ๋กœ ํ—ˆ์šฉํ•ด์•ผ ํ•˜๋Š” ๋„๋ฉ”์ธ ์ด๋ฆ„ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ํ•˜์œ„ ๋„๋ฉ”์ธ์— ๋Œ€ํ•ด `*.example.com`๊ณผ ๊ฐ™์€ ์™€์ผ๋“œ์นด๋“œ ๋„๋ฉ”์ธ์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์„ ํ—ˆ์šฉํ•˜๋ ค๋ฉด `allowed_hosts=[โ€œ*โ€]`๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ƒ๋žตํ•˜์„ธ์š”.
์ˆ˜์‹  ์š”์ฒญ์˜ ์œ ํšจ์„ฑ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ™•์ธ๋˜์ง€ ์•Š์œผ๋ฉด `400`์ด๋ผ๋Š” ์‘๋‹ต์ด ์ „์†ก๋ฉ๋‹ˆ๋‹ค.
## `GZipMiddleware`
`Accept-Encoding` ํ—ค๋”์— `โ€œgzipโ€`์ด ํฌํ•จ๋œ ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด GZip ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
๋ฏธ๋“ค์›จ์–ด๋Š” ํ‘œ์ค€ ์‘๋‹ต๊ณผ ์ŠคํŠธ๋ฆฌ๋ฐ ์‘๋‹ต์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
{* ../../docs_src/advanced_middleware/tutorial003.py hl[2,6] *}
์ง€์›๋˜๋Š” ์ธ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
* `minimum_size` - ์ด ์ตœ์†Œ ํฌ๊ธฐ(๋ฐ”์ดํŠธ)๋ณด๋‹ค ์ž‘์€ ์‘๋‹ต์€ GZipํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ `500`์ž…๋‹ˆ๋‹ค.
* `compresslevel` - GZip ์••์ถ• ์ค‘์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 1์—์„œ 9 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ `9`์ž…๋‹ˆ๋‹ค. ๊ฐ’์ด ๋‚ฎ์„์ˆ˜๋ก ์••์ถ• ์†๋„๋Š” ๋นจ๋ผ์ง€์ง€๋งŒ ํŒŒ์ผ ํฌ๊ธฐ๋Š” ์ปค์ง€๊ณ , ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ์••์ถ• ์†๋„๋Š” ๋Š๋ ค์ง€์ง€๋งŒ ํŒŒ์ผ ํฌ๊ธฐ๋Š” ์ž‘์•„์ง‘๋‹ˆ๋‹ค.
## ๊ธฐํƒ€ ๋ฏธ๋“ค์›จ์–ด
๋‹ค๋ฅธ ๋งŽ์€ ASGI ๋ฏธ๋“ค์›จ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด:
<a href=โ€œhttps://github.com/encode/uvicorn/blob/master/uvicorn/middleware/proxy_headers.pyโ€ class=โ€œexternal-linkโ€ target=โ€œ_blankโ€>์œ ๋น„์ฝ˜์˜ `ProxyHeadersMiddleware`></a>
<a href=โ€œhttps://github.com/florimondmanca/msgpack-asgiโ€ class=โ€œexternal-linkโ€ target=โ€œ_blankโ€>MessagePack</a>
์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด <a href=โ€œhttps://www.starlette.io/middleware/โ€ class=โ€œexternal-linkโ€ target=โ€œ_blankโ€>์Šคํƒ€๋ ›์˜ ๋ฏธ๋“ค์›จ์–ด ๋ฌธ์„œ</a> ๋ฐ <a href=โ€œhttps://github.com/florimondmanca/awesome-asgiโ€ class=โ€œexternal-linkโ€ target=โ€œ_blankโ€>ASGI Awesome List</a>๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.
Loadingโ€ฆ
Cancel
Save