hellorahulk commited on
Commit
7dd15cd
·
1 Parent(s): c70dbbf

[Cursor] Switch to simpler serverless handler

Browse files
Files changed (2) hide show
  1. index.py +58 -3
  2. requirements.txt +0 -1
index.py CHANGED
@@ -2,11 +2,11 @@ from flask import Flask, request, jsonify
2
  import json
3
  import os
4
  import tempfile
5
- from mangum import Mangum
6
  import magic
7
  import requests
8
  from werkzeug.utils import secure_filename
9
  import datetime
 
10
 
11
  app = Flask(__name__)
12
  app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB max file size
@@ -180,8 +180,63 @@ def parse_url():
180
  "details": str(e)
181
  }), 500
182
 
183
- # Create Mangum handler
184
- handler = Mangum(app, lifespan="off")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  # For local development
187
  if __name__ == '__main__':
 
2
  import json
3
  import os
4
  import tempfile
 
5
  import magic
6
  import requests
7
  from werkzeug.utils import secure_filename
8
  import datetime
9
+ import base64
10
 
11
  app = Flask(__name__)
12
  app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB max file size
 
180
  "details": str(e)
181
  }), 500
182
 
183
+ def handler(event, context):
184
+ """Serverless handler for Vercel"""
185
+
186
+ # Get HTTP method and path
187
+ http_method = event.get('httpMethod', 'GET')
188
+ path = event.get('path', '/')
189
+
190
+ # Handle OPTIONS request for CORS
191
+ if http_method == 'OPTIONS':
192
+ return {
193
+ 'statusCode': 204,
194
+ 'headers': {
195
+ 'Access-Control-Allow-Origin': '*',
196
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
197
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
198
+ },
199
+ 'body': ''
200
+ }
201
+
202
+ # Create test client
203
+ with app.test_client() as client:
204
+ # Prepare headers
205
+ headers = event.get('headers', {})
206
+
207
+ # Handle body
208
+ body = None
209
+ if 'body' in event:
210
+ if event.get('isBase64Encoded', False):
211
+ body = base64.b64decode(event['body'])
212
+ else:
213
+ body = event['body'].encode('utf-8') if event['body'] else None
214
+
215
+ # Make request to Flask app
216
+ response = client.open(
217
+ path,
218
+ method=http_method,
219
+ headers=headers,
220
+ data=body,
221
+ environ_base={
222
+ 'CONTENT_LENGTH': str(len(body)) if body else '0',
223
+ 'CONTENT_TYPE': headers.get('content-type', 'application/json')
224
+ }
225
+ )
226
+
227
+ # Prepare response
228
+ response_headers = dict(response.headers)
229
+ response_headers.update({
230
+ 'Access-Control-Allow-Origin': '*',
231
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
232
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
233
+ })
234
+
235
+ return {
236
+ 'statusCode': response.status_code,
237
+ 'headers': response_headers,
238
+ 'body': response.get_data(as_text=True)
239
+ }
240
 
241
  # For local development
242
  if __name__ == '__main__':
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  flask==2.0.1
2
  werkzeug==2.0.1
3
- mangum==0.17.0
4
  python-magic==0.4.27
5
  requests==2.31.0
 
1
  flask==2.0.1
2
  werkzeug==2.0.1
 
3
  python-magic==0.4.27
4
  requests==2.31.0