Files
LibraryAPI/library_service/settings.py

67 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Модуль настроек проекта"""
import os
from dotenv import load_dotenv
from fastapi import FastAPI
from sqlmodel import Session, create_engine
from toml import load
load_dotenv()
with open("pyproject.toml", 'r', encoding='utf-8') as f:
config = load(f)
def get_app() -> FastAPI:
"""Dependency, для получение экземплярра FastAPI application"""
return FastAPI(
title=config["tool"]["poetry"]["name"],
description=config["tool"]["poetry"]["description"],
version=config["tool"]["poetry"]["version"],
openapi_tags=[
{
"name": "authentication",
"description": "Авторизация пользователя."
},
{
"name": "authors",
"description": "Действия с авторами.",
},
{
"name": "books",
"description": "Действия с книгами.",
},
{
"name": "genres",
"description": "Действия с жанрами.",
},
{
"name": "relations",
"description": "Действия с связями.",
},
{
"name": "misc",
"description": "Прочие.",
},
],
)
HOST = os.getenv("POSTGRES_HOST")
PORT = os.getenv("POSTGRES_PORT")
USER = os.getenv("POSTGRES_USER")
PASSWORD = os.getenv("POSTGRES_PASSWORD")
DATABASE = os.getenv("POSTGRES_DB")
if not USER or not PASSWORD or not DATABASE or not HOST:
raise ValueError("Missing environment variables")
POSTGRES_DATABASE_URL = f"postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}"
engine = create_engine(POSTGRES_DATABASE_URL, echo=True, future=True)
def get_session():
"""Dependency, для получение сессии БД"""
with Session(engine) as session:
yield session