Files
saladeaula.digital/http-api/app.py
2025-04-01 19:15:10 -03:00

55 lines
1.8 KiB
Python

import os
from typing import Any
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler.api_gateway import (
APIGatewayHttpResolver,
CORSConfig,
Response,
content_types,
)
from aws_lambda_powertools.event_handler.exceptions import ServiceError
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
from middlewares import AuthorizerMiddleware, TenantMiddleware
from routes import courses, enrollments, lookup, orders, settings, users, webhooks
DEBUG = 'AWS_SAM_LOCAL' in os.environ
tracer = Tracer()
logger = Logger(__name__)
cors = CORSConfig(
allow_origin='*',
allow_headers=['Content-Type', 'X-Requested-With', 'Authorization', 'X-Tenant'],
max_age=600,
allow_credentials=False,
)
app = APIGatewayHttpResolver(enable_validation=True, cors=cors, debug=DEBUG)
app.use(middlewares=[AuthorizerMiddleware(), TenantMiddleware()])
app.include_router(courses.router, prefix='/courses')
app.include_router(enrollments.router, prefix='/enrollments')
app.include_router(orders.router, prefix='/orders')
app.include_router(users.router, prefix='/users')
app.include_router(webhooks.router, prefix='/webhooks')
app.include_router(settings.router, prefix='/settings')
app.include_router(lookup.router, prefix='/lookup')
@app.exception_handler(ServiceError)
def exc_error(exc: ServiceError):
return Response(
body={
'msg': exc.msg,
'err': type(exc).__name__,
},
content_type=content_types.APPLICATION_JSON,
status_code=exc.status_code,
)
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
@tracer.capture_lambda_handler
def lambda_handler(event: dict[str, Any], context: LambdaContext) -> dict[str, Any]:
return app.resolve(event, context)