anderson-ufrj commited on
Commit
05ab472
·
1 Parent(s): 7b9a6bc

fix: disable TrustedHostMiddleware for HuggingFace Spaces compatibility

Browse files

The middleware was causing 400 Bad Request errors due to proxy header issues

Files changed (1) hide show
  1. src/api/app.py +12 -11
src/api/app.py CHANGED
@@ -12,7 +12,7 @@ from typing import Dict, Any
12
 
13
  from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
14
  from fastapi.middleware.cors import CORSMiddleware
15
- from fastapi.middleware.trustedhost import TrustedHostMiddleware
16
  from fastapi.responses import JSONResponse
17
  from fastapi.openapi.docs import get_swagger_ui_html
18
  from fastapi.openapi.utils import get_openapi
@@ -128,16 +128,17 @@ app.add_middleware(
128
  )
129
 
130
  # Add trusted host middleware for production
131
- if settings.app_env == "production":
132
- app.add_middleware(
133
- TrustedHostMiddleware,
134
- allowed_hosts=["api.cidadao.ai", "*.cidadao.ai", "*.hf.space", "*.huggingface.co"]
135
- )
136
- else:
137
- app.add_middleware(
138
- TrustedHostMiddleware,
139
- allowed_hosts=["localhost", "127.0.0.1", "*.cidadao.ai", "testserver"]
140
- )
 
141
 
142
  # CORS middleware with secure configuration
143
  app.add_middleware(
 
12
 
13
  from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
14
  from fastapi.middleware.cors import CORSMiddleware
15
+ # from fastapi.middleware.trustedhost import TrustedHostMiddleware # Disabled for HuggingFace
16
  from fastapi.responses import JSONResponse
17
  from fastapi.openapi.docs import get_swagger_ui_html
18
  from fastapi.openapi.utils import get_openapi
 
128
  )
129
 
130
  # Add trusted host middleware for production
131
+ # DISABLED for HuggingFace Spaces - causes issues with proxy headers
132
+ # if settings.app_env == "production":
133
+ # app.add_middleware(
134
+ # TrustedHostMiddleware,
135
+ # allowed_hosts=["api.cidadao.ai", "*.cidadao.ai", "*.hf.space", "*.huggingface.co"]
136
+ # )
137
+ # else:
138
+ # app.add_middleware(
139
+ # TrustedHostMiddleware,
140
+ # allowed_hosts=["localhost", "127.0.0.1", "*.cidadao.ai", "testserver"]
141
+ # )
142
 
143
  # CORS middleware with secure configuration
144
  app.add_middleware(