refactor: improve CORS and streaming response headers
- Add configurable CORS origins - Remove duplicate CORS headers - Add X-Accel-Buffering header - Update env example file - Clean up header configurations
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
# HOST=0.0.0.0
|
# HOST=0.0.0.0
|
||||||
# PORT=9621
|
# PORT=9621
|
||||||
# NAMESPACE_PREFIX=lightrag # separating data from difference Lightrag instances
|
# NAMESPACE_PREFIX=lightrag # separating data from difference Lightrag instances
|
||||||
|
# CORS_ORIGINS=http://localhost:3000,http://localhost:8080
|
||||||
|
|
||||||
### Optional SSL Configuration
|
### Optional SSL Configuration
|
||||||
# SSL=true
|
# SSL=true
|
||||||
|
@@ -847,10 +847,19 @@ def create_app(args):
|
|||||||
lifespan=lifespan,
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_cors_origins():
|
||||||
|
"""Get allowed origins from environment variable
|
||||||
|
Returns a list of allowed origins, defaults to ["*"] if not set
|
||||||
|
"""
|
||||||
|
origins_str = os.getenv("CORS_ORIGINS", "*")
|
||||||
|
if origins_str == "*":
|
||||||
|
return ["*"]
|
||||||
|
return [origin.strip() for origin in origins_str.split(",")]
|
||||||
|
|
||||||
# Add CORS middleware
|
# Add CORS middleware
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=get_cors_origins(),
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
@@ -1377,10 +1386,7 @@ def create_app(args):
|
|||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
"Content-Type": "application/x-ndjson",
|
"Content-Type": "application/x-ndjson",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"X-Accel-Buffering": "no", # 确保在Nginx代理时正确处理流式响应
|
||||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
||||||
"Access-Control-Allow-Headers": "Content-Type",
|
|
||||||
"X-Accel-Buffering": "no", # Disable Nginx buffering
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@@ -316,9 +316,7 @@ class OllamaAPI:
|
|||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
"Content-Type": "application/x-ndjson",
|
"Content-Type": "application/x-ndjson",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"X-Accel-Buffering": "no", # 确保在Nginx代理时正确处理流式响应
|
||||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
||||||
"Access-Control-Allow-Headers": "Content-Type",
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -534,9 +532,7 @@ class OllamaAPI:
|
|||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
"Content-Type": "application/x-ndjson",
|
"Content-Type": "application/x-ndjson",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"X-Accel-Buffering": "no", # 确保在Nginx代理时正确处理流式响应
|
||||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
||||||
"Access-Control-Allow-Headers": "Content-Type",
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user