Skip to content

Commit

Permalink
added: fastapi-integration library
Browse files Browse the repository at this point in the history
  • Loading branch information
ManiMozaffar committed May 12, 2023
1 parent d307b27 commit e76b869
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 838 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ wcwidth==0.2.6
win32-setctime==1.1.0
wincertstore==0.2
alembic==1.10.2
aiohttp
aiohttp
fastapi-integration==0.1.0
23 changes: 5 additions & 18 deletions src/core/config.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
from functools import lru_cache
from typing import Dict, Type
from loguru import logger
from dotenv import load_dotenv

from core.settings.app import AppSettings
from core.settings.base import AppEnvTypes, BaseAppSettings
from core.settings.development import DevAppSettings
from core.settings.production import ProdAppSettings
from core.settings.test import TestAppSettings

environments: Dict[AppEnvTypes, Type[AppSettings]] = {
AppEnvTypes.prod: ProdAppSettings,
AppEnvTypes.dev: DevAppSettings,
AppEnvTypes.test: TestAppSettings,
}
from fastapi_integration import FastApiConfig


@lru_cache
def get_app_settings() -> AppSettings:
app_env = BaseAppSettings().app_env
logger.info(app_env)
config = environments[app_env]
return config()
def get_app_settings() -> FastApiConfig:
load_dotenv()
return FastApiConfig()
2 changes: 1 addition & 1 deletion src/core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from services.tech.repositories import router as tech_router


router = APIRouter()
router = APIRouter(prefix="/api")
router.include_router(ads_router, tags=["ads"], prefix="/ads")
router.include_router(proxy_router, tags=["proxy"], prefix="/proxy")
router.include_router(jobs_router, tags=["jobs"], prefix="/jobs")
Expand Down
29 changes: 4 additions & 25 deletions src/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from collections.abc import AsyncGenerator

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.orm import declarative_base
from loguru import logger
import redis
from fastapi_integration.db import SqlEngine

from core.config import get_app_settings

Expand All @@ -29,31 +29,10 @@ def get_connection(self) -> redis.Redis:
return redis.Redis(connection_pool=self.redis_pool)


engine = create_async_engine(
get_app_settings().database_url,
future=True,
echo=False,
)


async def get_db() -> AsyncGenerator:
AsyncSessionFactory = sessionmaker(
engine, autoflush=False, expire_on_commit=False, class_=AsyncSession
)
async with AsyncSessionFactory() as session:
logger.debug(f"ASYNC Pool: {engine.pool.status()}")
yield session


SQL_ENGINE = SqlEngine(get_app_settings())
REDIS_DB = RedisDatabase()
Base = declarative_base()


def get_redis_db():
return REDIS_DB.get_connection


def get_base():
return declarative_base()


Base = get_base()
55 changes: 14 additions & 41 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
import logging

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from starlette.middleware.cors import CORSMiddleware
import uvicorn
from fastapi_integration import FastAPIExtended, FastApiConfig

from core.handlers import http_error_handler, http422_error_handler
from core.config import get_app_settings
from core.events import create_start_app_handler, create_stop_app_handler
from core.routes import router


settings = get_app_settings()
settings.configure_logging()


def get_application() -> FastAPI:
application = FastAPI(**settings.fastapi_kwargs)

application.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_hosts,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

application.add_event_handler(
"startup",
create_start_app_handler(),
)
application.add_event_handler(
"shutdown",
create_stop_app_handler(),
)
application.add_exception_handler(HTTPException, http_error_handler)
application.add_exception_handler(
RequestValidationError, http422_error_handler
)
application.include_router(router, prefix=settings.api_prefix)
return application


app = get_application()
from db import Base, SQL_ENGINE


app = FastAPIExtended(
features=[
FastApiConfig,
],
db_engine=SQL_ENGINE,
routers=[
router
],
base=Base
)


def run_application():
Expand Down
Empty file removed src/orm/__init__.py
Empty file.
Loading

0 comments on commit e76b869

Please sign in to comment.