0

Adding CORS to Python FastAPI Framework is giving this error

error   error serving tap http server: http: Server closed

Configuration -

def get_app() -> FastAPI:
    """
    Returns the FastAPI ASGI object that will be later consumed by an uvicorn worker. This is where you register
    dependencies, route prefixes, swagger tags, and associate routers to the application.
    """
    app_config = get_config()  # get config and initialise logging

    app = FastAPI(title=APPLICATION_NAME, version="0.1.0")
    log.info(f"Starting {APPLICATION_NAME} version={app_config.app_version}!")

    app.include_router(home.router, tags=["home"])
    app.include_router(health.router, tags=["health"])
    app.include_router(content_relevancy_interface.router, tags=["content-relevancy-interface"])
    app.include_router(copperfield.router, tags=["copperfield"])
    origins = [
        "https://abcc,com",
        "http://abcc.net",
        "http://localhost",
        "http://localhost:8080",
    ]

    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    app.middleware('http')(catch_exceptions_middleware)  # register global exception handler
    slash_patcher(app)
    return app

This is how it is run in a shell script

export APP_RUN="gunicorn -c /etc/gunicorn/config.py seo_automation_common_apis.fastapi_app:get_app"
.
.
Some other setup 
# Start app
$APP_RUN

It seems the error is coming from istio -

https://github.com/istio/istio/issues/44445

https://github.com/istio/istio/issues/44244

Is istio used internally in FastApi ?

Let me know if any other detail is needed/missing.

Any help is appreciated

Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
  • 1
    You're gonna have to provide way more information for anyone to be able to help you. Provide enough code so that someone else can reproduce your error, how you’re running and testing it, and the full error traceback, not just the message. – M.O. Aug 04 '23 at 11:26
  • @M.O. I am not getting anything from the splunk logs. It seems to be something related to istio -> https://github.com/istio/istio/issues/44244 . I'll try to add a try catch block add the stacktrace to logs and see if I get something – Akshay Hazari Aug 04 '23 at 12:22
  • @M.O. I've updated the code , would you require anything else from me regarding the fastapi config/code ? – Akshay Hazari Aug 04 '23 at 12:23

0 Answers0