devcool20 commited on
Commit
34f4c74
·
verified ·
1 Parent(s): f46acd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -5
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import sys
3
  from flask import Flask, request, jsonify
4
- from flask_cors import CORS # Ensure Flask-CORS is imported
5
  import numpy as np
6
  import json
7
  import google.api_core.exceptions
@@ -18,10 +18,41 @@ load_dotenv()
18
 
19
  app = Flask(__name__)
20
 
21
- # --- CRITICAL FIX: CORS Configuration ---
22
- # This MUST EXACTLY match your Vercel frontend URL, WITHOUT a trailing slash.
23
- # We explicitly allow Content-Type and Authorization headers for preflight requests.
24
- CORS(app, resources={r"/*": {"origins": "https://sales-doc.vercel.app", "allow_headers": ["Content-Type", "Authorization"]}})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # --- Global Model Instances ---
27
  sales_agent = None
 
1
  import os
2
  import sys
3
  from flask import Flask, request, jsonify
4
+ from flask_cors import CORS # Keep this import, but we'll use it differently for now
5
  import numpy as np
6
  import json
7
  import google.api_core.exceptions
 
18
 
19
  app = Flask(__name__)
20
 
21
+ # --- AGGRESSIVE CORS CONFIGURATION ---
22
+ # This attempts to ensure CORS headers are *always* sent.
23
+ # Define your allowed origin explicitly.
24
+ ALLOWED_ORIGIN = "https://sales-doc.vercel.app"
25
+
26
+ @app.before_request
27
+ def handle_options_requests():
28
+ """Handle CORS preflight OPTIONS requests."""
29
+ if request.method == 'OPTIONS':
30
+ response = app.make_response('')
31
+ response.headers.add('Access-Control-Allow-Origin', ALLOWED_ORIGIN)
32
+ response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
33
+ response.headers.add('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
34
+ response.headers.add('Access-Control-Allow-Credentials', 'true') # If your frontend sends credentials
35
+ response.headers.add('Access-Control-Max-Age', '86400') # Cache preflight for 24 hours
36
+ print(f"DEBUG: Handling OPTIONS preflight request from {request.origin}")
37
+ print(f"DEBUG: Setting CORS headers for OPTIONS: {response.headers}")
38
+ return response
39
+
40
+ @app.after_request
41
+ def add_cors_headers(response):
42
+ """Add CORS headers to all responses."""
43
+ response.headers.add('Access-Control-Allow-Origin', ALLOWED_ORIGIN)
44
+ response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
45
+ response.headers.add('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
46
+ response.headers.add('Access-Control-Allow-Credentials', 'true')
47
+ print(f"DEBUG: Adding CORS headers to response for origin {request.origin if request.origin else 'N/A'}")
48
+ print(f"DEBUG: Response headers: {response.headers}")
49
+ return response
50
+
51
+ # You can comment out the Flask-CORS extension initialization if using manual headers,
52
+ # or keep it if it's not causing conflicts. For this aggressive approach, we'll
53
+ # rely on the manual headers.
54
+ # CORS(app, resources={r"/*": {"origins": ALLOWED_ORIGIN, "allow_headers": ["Content-Type", "Authorization"]}})
55
+
56
 
57
  # --- Global Model Instances ---
58
  sales_agent = None