forked from marketcalls/openalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors.py
More file actions
56 lines (41 loc) · 1.71 KB
/
cors.py
File metadata and controls
56 lines (41 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# cors.py
import os
from flask_cors import CORS
def get_cors_config():
"""
Get CORS configuration from environment variables.
Returns a dictionary with CORS configuration options.
"""
cors_config = {}
# Check if CORS is enabled
cors_enabled = os.getenv("CORS_ENABLED", "FALSE").upper() == "TRUE"
if not cors_enabled:
# If CORS is disabled, return empty config (will use Flask-CORS defaults)
return cors_config
# Get allowed origins
allowed_origins = os.getenv("CORS_ALLOWED_ORIGINS")
if allowed_origins:
cors_config["origins"] = [origin.strip() for origin in allowed_origins.split(",")]
# Get allowed methods
allowed_methods = os.getenv("CORS_ALLOWED_METHODS")
if allowed_methods:
cors_config["methods"] = [method.strip() for method in allowed_methods.split(",")]
# Get allowed headers
allowed_headers = os.getenv("CORS_ALLOWED_HEADERS")
if allowed_headers:
cors_config["allow_headers"] = [header.strip() for header in allowed_headers.split(",")]
# Get exposed headers
exposed_headers = os.getenv("CORS_EXPOSED_HEADERS")
if exposed_headers:
cors_config["expose_headers"] = [header.strip() for header in exposed_headers.split(",")]
# Check if credentials are allowed
credentials = os.getenv("CORS_ALLOW_CREDENTIALS", "FALSE").upper() == "TRUE"
if credentials:
cors_config["supports_credentials"] = True
# Max age for preflight requests
max_age = os.getenv("CORS_MAX_AGE")
if max_age and max_age.isdigit():
cors_config["max_age"] = int(max_age)
return cors_config
# Initialize Flask-CORS without the app object
cors = CORS(resources={r"/api/*": get_cors_config()})