Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use pydantic for config managment
  • Loading branch information
Kilo59 committed Apr 27, 2020
commit acbbcfeb3f4adacebacb9815d5d8cd31c227d3b0
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ dataclasses = {version = "*",markers = "python_version<'3.7'"}
fastapi = "*"
gunicorn = "*"
idna_ssl = {version = "*",markers = "python_version<'3.7'"}
pydantic = {extras = ["dotenv"],version = "*"}
python-dateutil = "*"
python-dotenv = "*"
requests = "*"
uvicorn = "*"

Expand Down
59 changes: 31 additions & 28 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""app.config.py"""
import functools
import logging

import pydantic

CFG_LOGGER = logging.getLogger("app.config")


class _Settings(pydantic.BaseSettings):
port: int = 5000


@functools.lru_cache()
def get_settings(**kwargs):
"""
Read settings from the environment or `.env` file.
https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support

Usage:
import app.config

settings = app.config.get_settings(_env_file="")
port_number = settings.port
"""
CFG_LOGGER.info("Loading Config settings from Environment ...")
return _Settings(**kwargs)
Empty file removed app/config/__init__.py
Empty file.
10 changes: 0 additions & 10 deletions app/config/settings.py

This file was deleted.

6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
app.main.py
"""
import logging
import os

import pydantic
import uvicorn
Expand All @@ -11,6 +10,7 @@
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import JSONResponse

from .config import get_settings
from .data import data_source
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session
Expand All @@ -20,6 +20,8 @@
# ############
LOGGER = logging.getLogger("api")

SETTINGS = get_settings()

APP = FastAPI(
title="Coronavirus Tracker",
description=(
Expand Down Expand Up @@ -93,5 +95,5 @@ async def handle_validation_error(
# Running of app.
if __name__ == "__main__":
uvicorn.run(
"app.main:APP", host="127.0.0.1", port=int(os.getenv("PORT", "5000")), log_level="info",
"app.main:APP", host="127.0.0.1", port=SETTINGS.port, log_level="info",
)
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ pluggy==0.13.1
py==1.8.1
pylint==2.4.4
pyparsing==2.4.7
pytest-asyncio==0.10.0
pytest-asyncio==0.11.0
pytest-cov==2.8.1
pytest==5.4.1
pyyaml==5.3.1
regex==2020.4.4
requests==2.23.0
responses==0.10.12
responses==0.10.14
six==1.14.0
smmap==3.0.2
stevedore==1.32.0
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ httptools==0.1.1 ; sys_platform != 'win32' and sys_platform != 'cygwin' and plat
idna-ssl==1.1.0 ; python_version < '3.7'
idna==2.9
multidict==4.7.5
pydantic==1.5
pydantic[dotenv]==1.5.1
python-dateutil==2.8.1
python-dotenv==0.13.0
requests==2.23.0
Expand Down