#####################################################################
# THIS FILE IS AUTOMATICALLY GENERATED BY UNSTRUCTURED API TOOLS.
# DO NOT MODIFY DIRECTLY
#####################################################################


from fastapi import FastAPI, Request, status
import logging
import os

{% for module in module_names -%}
from .{{ module }} import router as {{module}}_router
{% endfor %}

app = FastAPI(
  title="{{ title }}",
  description="""{{ description }}""",
  version="{{ version or '1.0.0' }}",
  docs_url="{{ '/' ~ version_name ~ '/docs' if version_name else '/docs' }}",
  openapi_url="{{ '/' ~ version_name ~ '/openapi.json' if version_name else '/openapi.json' }}"
)

allowed_origins = os.environ.get("ALLOWED_ORIGINS", None)
if allowed_origins:
    from fastapi.middleware.cors import CORSMiddleware
    app.add_middleware(
        CORSMiddleware,
        allow_origins=allowed_origins,
        allow_methods=["OPTIONS", "POST"],
        allow_headers=["Content-Type"]
        )

{% for module in module_names -%}
app.include_router({{ module }}_router)
{% endfor %}

# Filter out /healthcheck noise
class HealthCheckFilter(logging.Filter):
    def filter(self, record: logging.LogRecord) -> bool:
        return record.getMessage().find("/healthcheck") == -1

logging.getLogger("uvicorn.access").addFilter(HealthCheckFilter())

@app.get("/healthcheck", status_code=status.HTTP_200_OK, include_in_schema=False)
def healthcheck(request: Request):
    return {"healthcheck": "HEALTHCHECK STATUS: EVERYTHING OK!"}
