grillazz / fastapi-sqlalchemy-asyncpg

Integration of FastAPI framework supported by Pydantic with SQLAlchemy ORM and PostgreSQL on asyncpg driver
MIT License
415 stars 57 forks source link

Discuss about config.py #108

Closed h2appy closed 1 year ago

h2appy commented 1 year ago
import os
from functools import lru_cache

from pydantic import PostgresDsn, RedisDsn
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    asyncpg_url: PostgresDsn = os.getenv("SQL_URL")
    secret_key: str = os.getenv("FERNET_KEY")
    redis_url: RedisDsn = os.getenv("REDIS_URL")
    jwt_algorithm: str = os.getenv("JWT_ALGORITHM")
    jwt_expire: int = os.getenv("JWT_EXPIRE")

@lru_cache
def get_settings():
    return Settings()

settings = get_settings()

It's the same as below, I think. Because get_setting() with @lru_cache will always return the same object.:

import os
from functools import lru_cache

from pydantic import PostgresDsn, RedisDsn
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    asyncpg_url: PostgresDsn = os.getenv("SQL_URL")
    secret_key: str = os.getenv("FERNET_KEY")
    redis_url: RedisDsn = os.getenv("REDIS_URL")
    jwt_algorithm: str = os.getenv("JWT_ALGORITHM")
    jwt_expire: int = os.getenv("JWT_EXPIRE")

settings = Setting()
grillazz commented 1 year ago

@h2appy thx for rising this. Think it is left over from older fastapi versions. In case of settings = Setting() it will be same object in memory so PR most welcome.