main.py 1.31 KB
import logging

from typing import Any, Union

from fastapi import FastAPI
from fastapi.middleware import Middleware
from starlette.responses import JSONResponse
from starlette.requests import HTTPConnection, Request

from starlette_context import context, plugins
from starlette_context.middleware import RawContextMiddleware

from aiocache import caches

from presupuestov1.routers import (
    project,
    entity
)


logging.basicConfig(
	level=logging.INFO,
	format='%(asctime)s %(message)s',
	filename='log.log',
)
logging.getLogger('api_proy2').setLevel(logging.DEBUG)


app = FastAPI(
    title='Jorvi API',
    middleware=[
        Middleware(
            RawContextMiddleware,
            plugins=(
                plugins.RequestIdPlugin(),
            )
        )
    ],
    root_path='/api'
)
app.include_router(project.router)
app.include_router(entity.router)


@app.on_event('startup')
async def startup_event():
    caches.set_config({
        'default': {
            'cache': 'aiocache.SimpleMemoryCache',
            'serializer': {
                'class': 'aiocache.serializers.StringSerializer' # Cache raw text
            }
        }
    })

@app.on_event('shutdown')
async def shutdown_event():
    default_cache = caches.get('default')
    if hasattr(default_cache, 'close'):
        await default_cache.close()