main.py
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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,
classifier,
daily,
)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(message)s',
filename='log.log',
)
logging.getLogger('api_proy2').setLevel(logging.DEBUG)
app = FastAPI(
title='MEFP presupuestosv1 API',
middleware=[
Middleware(
RawContextMiddleware,
plugins=(
plugins.RequestIdPlugin(),
)
)
],
root_path='/api'
)
app.include_router(project.router)
app.include_router(entity.router)
app.include_router(classifier.router_objeto)
app.include_router(classifier.router_acteco)
app.include_router(classifier.router_finfun)
app.include_router(daily.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()