-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (53 loc) · 2.35 KB
/
main.py
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
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
import logging
import uvicorn
from api.api_v1.api import api_router
from core.config import settings
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
from fastapi.openapi.utils import get_openapi
def api_factory():
app = FastAPI(title=settings.PROJECT_NAME,
root_path="/Template",
version='0.0.1',
description='Template para criação de APIs',
)
logging.config.dictConfig(settings.LOGGING_CONFIG)
LoggingInstrumentor().instrument()
HTTPXClientInstrumentor().instrument()
# Set all CORS enabled origins
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin)
for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=settings.API_V1_STR)
return app
app = api_factory()
@app.get(f"{app.root_path}/", description='Resposta somente para validar se a API subiu corretamente. Sem nenhuma conexão com o banco de dados.',
summary='Valida se API está no ar')
def get_index():
return {'msg': 'API está no ar!'}
@app.get(f"{app.root_path}/docs", include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(openapi_url="/Template/openapi.json", title='API Docs')
# Rota para a documentação Redoc
@app.get(f"{app.root_path}/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(openapi_url="/Template/openapi.json", title='ReDoc')
'''Rota para o esquema OpenAPI'''
@app.get(f"{app.root_path}/openapi.json", include_in_schema=False)
async def get_custom_openapi():
return get_openapi(title=app.title, version="0.0.1", routes=app.routes, description=app.description)
def run():
log_config = uvicorn.config.LOGGING_CONFIG
log_config["formatters"]["access"]["fmt"] = settings.LOGGING_CONFIG["formatters"]["standard"]["format"]
uvicorn.run("main:app", log_config=log_config, reload=True)
if __name__ == "__main__":
run()