-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
59 lines (52 loc) · 1.75 KB
/
Copy pathindex.py
File metadata and controls
59 lines (52 loc) · 1.75 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
57
58
59
import json
import logging
import secrets
from vercel import init_vercel
logger = logging.getLogger(__name__)
# Initialize Vercel environment
init_vercel()
# Import after Vercel env initialization because app import validates secrets.
from app import app
def handler(request):
"""Handle Vercel serverless function requests."""
try:
with app.test_client() as test_client:
# Convert Vercel request to Flask request context
method = request.get('method', 'GET')
path = request.get('path', '/')
headers = request.get('headers', {})
body = request.get('body', '')
# Make the request to Flask app
response = test_client.open(
path,
method=method,
headers=headers,
data=body
)
# Return response in Vercel format
return {
'statusCode': response.status_code,
'headers': dict(response.headers),
'body': response.get_data(as_text=True)
}
except Exception as error:
request_id = f"req_{secrets.token_urlsafe(12)}"
logger.error(
"Vercel request handling failed request_id=%s type=%s",
request_id,
type(error).__name__,
)
return {
'statusCode': 500,
'body': json.dumps(
{
'error': 'internal_error',
'message': 'An unexpected error occurred.',
'request_id': request_id,
}
),
'headers': {
'Content-Type': 'application/json',
'X-Request-ID': request_id,
}
}