hellorahulk commited on
Commit
159d092
·
1 Parent(s): 8b5c234

[Cursor] Simplify serverless handler

Browse files
Files changed (1) hide show
  1. api/index.py +174 -112
api/index.py CHANGED
@@ -1,10 +1,8 @@
1
- from http.server import BaseHTTPRequestHandler
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
 
10
  def is_valid_file(file_data):
@@ -60,102 +58,164 @@ def download_file(url):
60
  except Exception as e:
61
  raise ValueError(f"Failed to download file: {str(e)}")
62
 
63
- class Handler(BaseHTTPRequestHandler):
64
- def do_GET(self):
65
- if self.path == '/' or self.path == '':
66
- self.send_response(200)
67
- self.send_header('Content-type', 'application/json')
68
- self.end_headers()
69
- response = {
70
- "status": "ok",
71
- "message": "Document Processing API",
72
- "version": "1.0.0"
73
- }
74
- self.wfile.write(json.dumps(response).encode())
75
- elif self.path == '/health':
76
- self.send_response(200)
77
- self.send_header('Content-type', 'application/json')
78
- self.end_headers()
79
- response = {
80
- "status": "healthy",
81
- "timestamp": str(datetime.datetime.now(datetime.UTC))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  }
83
- self.wfile.write(json.dumps(response).encode())
84
- else:
85
- self.send_response(404)
86
- self.send_header('Content-type', 'application/json')
87
- self.end_headers()
88
- response = {
89
- "error": "Not Found",
90
- "details": f"Path {self.path} not found"
 
91
  }
92
- self.wfile.write(json.dumps(response).encode())
93
-
94
- def do_POST(self):
95
- content_length = int(self.headers.get('Content-Length', 0))
96
- post_data = self.rfile.read(content_length)
97
-
98
- if self.path == '/parse/file':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  try:
100
- if not post_data:
101
- self.send_error(400, "No file provided")
102
- return
103
-
104
- if not is_valid_file(post_data):
105
- self.send_error(400, "Invalid file type")
106
- return
107
-
108
- fd, temp_path = tempfile.mkstemp()
109
- os.close(fd)
110
-
111
- try:
112
- with open(temp_path, 'wb') as f:
113
- f.write(post_data)
114
-
115
- self.send_response(200)
116
- self.send_header('Content-type', 'application/json')
117
- self.end_headers()
118
- response = {
119
  "status": "success",
120
  "message": "File processed successfully",
121
  "metadata": {
122
  "size": os.path.getsize(temp_path),
123
  "mime_type": magic.from_file(temp_path, mime=True)
124
  }
125
- }
126
- self.wfile.write(json.dumps(response).encode())
127
- finally:
128
- try:
129
- os.unlink(temp_path)
130
- except:
131
- pass
132
-
133
- except Exception as e:
134
- self.send_error(500, str(e))
135
-
136
- elif self.path == '/parse/url':
137
- try:
138
  try:
139
- body = json.loads(post_data)
140
  except:
141
- self.send_error(400, "Invalid JSON")
142
- return
143
-
144
- url = body.get('url')
145
- if not url:
146
- self.send_error(400, "No URL provided")
147
- return
148
-
149
- if not url.startswith(('http://', 'https://')):
150
- self.send_error(400, "Invalid URL")
151
- return
152
-
153
- temp_path, content_type = download_file(url)
154
- try:
155
- self.send_response(200)
156
- self.send_header('Content-type', 'application/json')
157
- self.end_headers()
158
- response = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  "status": "success",
160
  "message": "URL processed successfully",
161
  "metadata": {
@@ -163,29 +223,31 @@ class Handler(BaseHTTPRequestHandler):
163
  "content_type": content_type,
164
  "size": os.path.getsize(temp_path)
165
  }
166
- }
167
- self.wfile.write(json.dumps(response).encode())
168
- finally:
169
- try:
170
- os.unlink(temp_path)
171
- except:
172
- pass
173
-
174
- except ValueError as e:
175
- self.send_error(400, str(e))
176
- except Exception as e:
177
- self.send_error(500, str(e))
178
-
179
  else:
180
- self.send_error(404, f"Path {self.path} not found")
181
-
182
- def do_OPTIONS(self):
183
- self.send_response(204)
184
- self.send_header('Access-Control-Allow-Origin', '*')
185
- self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
186
- self.send_header('Access-Control-Allow-Headers', 'Content-Type, Authorization')
187
- self.end_headers()
188
-
189
- def handler(request, context):
190
- """Vercel serverless handler"""
191
- return Handler(request, None, None).handle_request()
 
 
 
 
 
 
 
 
1
  import json
2
  import os
3
  import tempfile
4
  import magic
5
  import requests
 
6
  import datetime
7
 
8
  def is_valid_file(file_data):
 
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 = {
66
+ "Access-Control-Allow-Origin": "*",
67
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
68
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
69
+ "Content-Type": "application/json"
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": {
 
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,
238
+ "headers": cors_headers,
239
+ "body": json.dumps({
240
+ "error": "Not Found",
241
+ "details": f"Path {path} not found"
242
+ })
243
+ }
244
+
245
+ except Exception as e:
246
+ return {
247
+ "statusCode": 500,
248
+ "headers": cors_headers,
249
+ "body": json.dumps({
250
+ "error": "Internal Server Error",
251
+ "details": str(e)
252
+ })
253
+ }