main.py
2.17 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import re
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 starlette.middleware import Middleware
from starlette_caches.middleware import CacheMiddleware
from starlette_caches.rules import Rule
from presupuestov1 import cache
from presupuestov1.routers import (
project,
entity,
classifier,
classifier_income,
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(),
)
),
Middleware(
CacheMiddleware,
cache=cache.cache,
rules=[
Rule(match=re.compile(r"^/api/proyecto/.*"), ttl=0),
Rule(match=re.compile(r'^/api/.+'), ttl=86400, status=200),
],
),
],
root_path='/api'
)
app.include_router(project.router)
app.include_router(entity.router_entidad)
app.include_router(entity.router_ubigeo)
app.include_router(entity.router_da)
app.include_router(classifier.router_objeto)
app.include_router(classifier.router_acteco)
app.include_router(classifier.router_finfun)
app.include_router(classifier_income.router_rubro)
app.include_router(classifier_income.router_organismo)
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()