Skip to content

Commit acbbcfe

Browse files
committed
use pydantic for config managment
1 parent c6a6bae commit acbbcfe

File tree

8 files changed

+66
-44
lines changed

8 files changed

+66
-44
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ dataclasses = {version = "*",markers = "python_version<'3.7'"}
2727
fastapi = "*"
2828
gunicorn = "*"
2929
idna_ssl = {version = "*",markers = "python_version<'3.7'"}
30+
pydantic = {extras = ["dotenv"],version = "*"}
3031
python-dateutil = "*"
31-
python-dotenv = "*"
3232
requests = "*"
3333
uvicorn = "*"
3434

Pipfile.lock

Lines changed: 31 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/config.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""app.config.py"""
2+
import functools
3+
import logging
4+
5+
import pydantic
6+
7+
CFG_LOGGER = logging.getLogger("app.config")
8+
9+
10+
class _Settings(pydantic.BaseSettings):
11+
port: int = 5000
12+
13+
14+
@functools.lru_cache()
15+
def get_settings(**kwargs):
16+
"""
17+
Read settings from the environment or `.env` file.
18+
https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support
19+
20+
Usage:
21+
import app.config
22+
23+
settings = app.config.get_settings(_env_file="")
24+
port_number = settings.port
25+
"""
26+
CFG_LOGGER.info("Loading Config settings from Environment ...")
27+
return _Settings(**kwargs)

app/config/__init__.py

Whitespace-only changes.

app/config/settings.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
app.main.py
33
"""
44
import logging
5-
import os
65

76
import pydantic
87
import uvicorn
@@ -11,6 +10,7 @@
1110
from fastapi.middleware.gzip import GZipMiddleware
1211
from fastapi.responses import JSONResponse
1312

13+
from .config import get_settings
1414
from .data import data_source
1515
from .routers import V1, V2
1616
from .utils.httputils import setup_client_session, teardown_client_session
@@ -20,6 +20,8 @@
2020
# ############
2121
LOGGER = logging.getLogger("api")
2222

23+
SETTINGS = get_settings()
24+
2325
APP = FastAPI(
2426
title="Coronavirus Tracker",
2527
description=(
@@ -93,5 +95,5 @@ async def handle_validation_error(
9395
# Running of app.
9496
if __name__ == "__main__":
9597
uvicorn.run(
96-
"app.main:APP", host="127.0.0.1", port=int(os.getenv("PORT", "5000")), log_level="info",
98+
"app.main:APP", host="127.0.0.1", port=SETTINGS.port, log_level="info",
9799
)

requirements-dev.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ pluggy==0.13.1
3131
py==1.8.1
3232
pylint==2.4.4
3333
pyparsing==2.4.7
34-
pytest-asyncio==0.10.0
34+
pytest-asyncio==0.11.0
3535
pytest-cov==2.8.1
3636
pytest==5.4.1
3737
pyyaml==5.3.1
3838
regex==2020.4.4
3939
requests==2.23.0
40-
responses==0.10.12
40+
responses==0.10.14
4141
six==1.14.0
4242
smmap==3.0.2
4343
stevedore==1.32.0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ httptools==0.1.1 ; sys_platform != 'win32' and sys_platform != 'cygwin' and plat
1515
idna-ssl==1.1.0 ; python_version < '3.7'
1616
idna==2.9
1717
multidict==4.7.5
18-
pydantic==1.5
18+
pydantic[dotenv]==1.5.1
1919
python-dateutil==2.8.1
2020
python-dotenv==0.13.0
2121
requests==2.23.0

0 commit comments

Comments
 (0)