mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 13:27:45 -04:00
📝 Add documentation about settings and env vars (#1118)
* Add doc and example for env var config * Syntax highlight for .env file * Add test for configuration docs * 📝 Update settings docs, add more examples * ✅ Add tests for settings * 🚚 Rename "Application Configuration" to "Metadata and Docs URLs" to disambiguate between that and settings * 🔥 Remove replaced example file Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
0
docs_src/settings/app01/__init__.py
Normal file
0
docs_src/settings/app01/__init__.py
Normal file
10
docs_src/settings/app01/config.py
Normal file
10
docs_src/settings/app01/config.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
|
||||
settings = Settings()
|
||||
14
docs_src/settings/app01/main.py
Normal file
14
docs_src/settings/app01/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from . import config
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info():
|
||||
return {
|
||||
"app_name": config.settings.app_name,
|
||||
"admin_email": config.settings.admin_email,
|
||||
"items_per_user": config.settings.items_per_user,
|
||||
}
|
||||
0
docs_src/settings/app02/__init__.py
Normal file
0
docs_src/settings/app02/__init__.py
Normal file
10
docs_src/settings/app02/config.py
Normal file
10
docs_src/settings/app02/config.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
|
||||
settings: Settings = None
|
||||
21
docs_src/settings/app02/main.py
Normal file
21
docs_src/settings/app02/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from . import config
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def get_settings():
|
||||
if config.settings:
|
||||
return config.settings
|
||||
config.settings = config.Settings()
|
||||
return config.settings
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: config.Settings = Depends(get_settings)):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
23
docs_src/settings/app02/test_main.py
Normal file
23
docs_src/settings/app02/test_main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from . import config, main
|
||||
|
||||
client = TestClient(main.app)
|
||||
|
||||
|
||||
def get_settings_override():
|
||||
return config.Settings(admin_email="testing_admin@example.com")
|
||||
|
||||
|
||||
main.app.dependency_overrides[main.get_settings] = get_settings_override
|
||||
|
||||
|
||||
def test_app():
|
||||
|
||||
response = client.get("/info")
|
||||
data = response.json()
|
||||
assert data == {
|
||||
"app_name": "Awesome API",
|
||||
"admin_email": "testing_admin@example.com",
|
||||
"items_per_user": 50,
|
||||
}
|
||||
0
docs_src/settings/app03/__init__.py
Normal file
0
docs_src/settings/app03/__init__.py
Normal file
13
docs_src/settings/app03/config.py
Normal file
13
docs_src/settings/app03/config.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
|
||||
settings: Settings = None
|
||||
21
docs_src/settings/app03/main.py
Normal file
21
docs_src/settings/app03/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from . import config
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def get_settings():
|
||||
if config.settings:
|
||||
return config.settings
|
||||
config.settings = config.Settings()
|
||||
return config.settings
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: config.Settings = Depends(get_settings)):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
21
docs_src/settings/tutorial001.py
Normal file
21
docs_src/settings/tutorial001.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
|
||||
settings = Settings()
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info():
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
Reference in New Issue
Block a user