hellorahulk commited on
Commit
3985ce1
·
1 Parent(s): 159d092

[Cursor] Simplify serverless handler

Browse files
Files changed (1) hide show
  1. api/index.py +96 -148
api/index.py CHANGED
@@ -58,8 +58,73 @@ def download_file(url):
58
  except Exception as e:
59
  raise ValueError(f"Failed to download file: {str(e)}")
60
 
61
- def handler(event, context):
62
- """Serverless handler for Vercel"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  # Add CORS headers to all responses
65
  cors_headers = {
@@ -70,168 +135,36 @@ def handler(event, context):
70
  }
71
 
72
  # Handle OPTIONS request for CORS
73
- if event.get('httpMethod') == 'OPTIONS':
74
  return {
75
  "statusCode": 204,
76
  "headers": cors_headers,
77
  "body": ""
78
  }
79
 
80
- # Get path and method
81
- path = event.get('path', '/').rstrip('/')
82
- method = event.get('httpMethod', 'GET')
83
-
84
  try:
 
 
 
 
85
  # Route request
 
86
  if path == '' or path == '/':
87
- return {
88
- "statusCode": 200,
89
- "headers": cors_headers,
90
- "body": json.dumps({
91
- "status": "ok",
92
- "message": "Document Processing API",
93
- "version": "1.0.0"
94
- })
95
- }
96
-
97
  elif path == '/health':
98
- return {
99
- "statusCode": 200,
100
- "headers": cors_headers,
101
- "body": json.dumps({
102
- "status": "healthy",
103
- "timestamp": str(datetime.datetime.now(datetime.UTC))
104
- })
105
- }
106
-
107
  elif path == '/parse/file' and method == 'POST':
108
- # Check if there's a file in the request
109
- if 'body' not in event or not event['body']:
110
- return {
111
- "statusCode": 400,
112
- "headers": cors_headers,
113
- "body": json.dumps({
114
- "error": "No file provided",
115
- "details": "Please provide a file in the request body"
116
- })
117
- }
118
-
119
- # Get file data
120
- file_data = event['body']
121
- if event.get('isBase64Encoded', False):
122
  import base64
123
  file_data = base64.b64decode(file_data)
124
-
125
- # Validate file type
126
- if not is_valid_file(file_data):
127
- return {
128
- "statusCode": 400,
129
- "headers": cors_headers,
130
- "body": json.dumps({
131
- "error": "Invalid file type",
132
- "details": "Supported formats: PDF, TXT, HTML, MD, DOCX"
133
- })
134
- }
135
-
136
- # Save to temp file
137
- fd, temp_path = tempfile.mkstemp()
138
- os.close(fd)
139
-
140
- try:
141
- with open(temp_path, 'wb') as f:
142
- f.write(file_data)
143
-
144
- # Process file here
145
- return {
146
- "statusCode": 200,
147
- "headers": cors_headers,
148
- "body": json.dumps({
149
- "status": "success",
150
- "message": "File processed successfully",
151
- "metadata": {
152
- "size": os.path.getsize(temp_path),
153
- "mime_type": magic.from_file(temp_path, mime=True)
154
- }
155
- })
156
- }
157
- finally:
158
- # Clean up temp file
159
- try:
160
- os.unlink(temp_path)
161
- except:
162
- pass
163
-
164
  elif path == '/parse/url' and method == 'POST':
165
- # Get request body
166
- if 'body' not in event or not event['body']:
167
- return {
168
- "statusCode": 400,
169
- "headers": cors_headers,
170
- "body": json.dumps({
171
- "error": "No request body",
172
- "details": "Please provide a URL in the request body"
173
- })
174
- }
175
-
176
- # Parse JSON body
177
  try:
178
- body = json.loads(event['body'])
179
  except:
180
- return {
181
- "statusCode": 400,
182
- "headers": cors_headers,
183
- "body": json.dumps({
184
- "error": "Invalid JSON",
185
- "details": "Request body must be valid JSON"
186
- })
187
- }
188
-
189
- # Get URL from body
190
- url = body.get('url')
191
- if not url:
192
- return {
193
- "statusCode": 400,
194
- "headers": cors_headers,
195
- "body": json.dumps({
196
- "error": "No URL provided",
197
- "details": "Please provide a URL in the request body"
198
- })
199
- }
200
-
201
- if not url.startswith(('http://', 'https://')):
202
- return {
203
- "statusCode": 400,
204
- "headers": cors_headers,
205
- "body": json.dumps({
206
- "error": "Invalid URL",
207
- "details": "URL must start with http:// or https://"
208
- })
209
- }
210
-
211
- # Download and process file
212
- temp_path, content_type = download_file(url)
213
- try:
214
- # Process file here
215
- return {
216
- "statusCode": 200,
217
- "headers": cors_headers,
218
- "body": json.dumps({
219
- "status": "success",
220
- "message": "URL processed successfully",
221
- "metadata": {
222
- "url": url,
223
- "content_type": content_type,
224
- "size": os.path.getsize(temp_path)
225
- }
226
- })
227
- }
228
- finally:
229
- # Clean up temp file
230
- try:
231
- os.unlink(temp_path)
232
- except:
233
- pass
234
-
235
  else:
236
  return {
237
  "statusCode": 404,
@@ -242,6 +175,21 @@ def handler(event, context):
242
  })
243
  }
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  except Exception as e:
246
  return {
247
  "statusCode": 500,
 
58
  except Exception as e:
59
  raise ValueError(f"Failed to download file: {str(e)}")
60
 
61
+ def handle_root():
62
+ return {
63
+ "status": "ok",
64
+ "message": "Document Processing API",
65
+ "version": "1.0.0"
66
+ }
67
+
68
+ def handle_health():
69
+ return {
70
+ "status": "healthy",
71
+ "timestamp": str(datetime.datetime.now(datetime.UTC))
72
+ }
73
+
74
+ def handle_parse_file(file_data):
75
+ if not file_data:
76
+ raise ValueError("No file provided")
77
+
78
+ if not is_valid_file(file_data):
79
+ raise ValueError("Invalid file type")
80
+
81
+ fd, temp_path = tempfile.mkstemp()
82
+ os.close(fd)
83
+
84
+ try:
85
+ with open(temp_path, 'wb') as f:
86
+ f.write(file_data)
87
+
88
+ return {
89
+ "status": "success",
90
+ "message": "File processed successfully",
91
+ "metadata": {
92
+ "size": os.path.getsize(temp_path),
93
+ "mime_type": magic.from_file(temp_path, mime=True)
94
+ }
95
+ }
96
+ finally:
97
+ try:
98
+ os.unlink(temp_path)
99
+ except:
100
+ pass
101
+
102
+ def handle_parse_url(url):
103
+ if not url:
104
+ raise ValueError("No URL provided")
105
+
106
+ if not url.startswith(('http://', 'https://')):
107
+ raise ValueError("Invalid URL")
108
+
109
+ temp_path, content_type = download_file(url)
110
+ try:
111
+ return {
112
+ "status": "success",
113
+ "message": "URL processed successfully",
114
+ "metadata": {
115
+ "url": url,
116
+ "content_type": content_type,
117
+ "size": os.path.getsize(temp_path)
118
+ }
119
+ }
120
+ finally:
121
+ try:
122
+ os.unlink(temp_path)
123
+ except:
124
+ pass
125
+
126
+ def handler(request, context):
127
+ """Vercel serverless handler"""
128
 
129
  # Add CORS headers to all responses
130
  cors_headers = {
 
135
  }
136
 
137
  # Handle OPTIONS request for CORS
138
+ if request.get('httpMethod') == 'OPTIONS':
139
  return {
140
  "statusCode": 204,
141
  "headers": cors_headers,
142
  "body": ""
143
  }
144
 
 
 
 
 
145
  try:
146
+ # Get path and method
147
+ path = request.get('path', '/').rstrip('/')
148
+ method = request.get('httpMethod', 'GET')
149
+
150
  # Route request
151
+ response_data = None
152
  if path == '' or path == '/':
153
+ response_data = handle_root()
 
 
 
 
 
 
 
 
 
154
  elif path == '/health':
155
+ response_data = handle_health()
 
 
 
 
 
 
 
 
156
  elif path == '/parse/file' and method == 'POST':
157
+ file_data = request.get('body', '')
158
+ if request.get('isBase64Encoded', False):
 
 
 
 
 
 
 
 
 
 
 
 
159
  import base64
160
  file_data = base64.b64decode(file_data)
161
+ response_data = handle_parse_file(file_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  elif path == '/parse/url' and method == 'POST':
 
 
 
 
 
 
 
 
 
 
 
 
163
  try:
164
+ body = json.loads(request.get('body', '{}'))
165
  except:
166
+ raise ValueError("Invalid JSON")
167
+ response_data = handle_parse_url(body.get('url'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  else:
169
  return {
170
  "statusCode": 404,
 
175
  })
176
  }
177
 
178
+ return {
179
+ "statusCode": 200,
180
+ "headers": cors_headers,
181
+ "body": json.dumps(response_data)
182
+ }
183
+
184
+ except ValueError as e:
185
+ return {
186
+ "statusCode": 400,
187
+ "headers": cors_headers,
188
+ "body": json.dumps({
189
+ "error": "Bad Request",
190
+ "details": str(e)
191
+ })
192
+ }
193
  except Exception as e:
194
  return {
195
  "statusCode": 500,