|
| 1 | +import os |
| 2 | +import ssl |
| 3 | +from flask import Flask, request, jsonify |
| 4 | +from flask_sqlalchemy import SQLAlchemy |
| 5 | +from io import BytesIO |
| 6 | + |
| 7 | +app = Flask(__name__) |
| 8 | + |
| 9 | +ssl_context = ssl.create_default_context() |
| 10 | +ssl_context.check_hostname = False |
| 11 | +ssl_context.verify_mode = ssl.CERT_NONE |
| 12 | + |
| 13 | +db_url = os.environ.get('DATABASE_URL') |
| 14 | +app.config['SQLALCHEMY_DATABASE_URI'] = db_url |
| 15 | +app.config['SQLALCHEMY_ENGINE_OPTIONS'] = { |
| 16 | + 'connect_args': {'ssl_context': ssl_context} |
| 17 | +} |
| 18 | +db = SQLAlchemy(app) |
| 19 | + |
| 20 | +class Task(db.Model): |
| 21 | + id = db.Column(db.Integer, primary_key=True) |
| 22 | + content = db.Column(db.String(200), nullable=False) |
| 23 | + |
| 24 | +@app.route('/tasks', methods=['GET', 'POST']) |
| 25 | +def tasks(): |
| 26 | + with app.app_context(): |
| 27 | + db.create_all() |
| 28 | + if request.method == 'POST': |
| 29 | + data = request.get_json() |
| 30 | + new_task = Task(content=data['content']) |
| 31 | + db.session.add(new_task) |
| 32 | + db.session.commit() |
| 33 | + return jsonify({"message": "Task added"}), 201 |
| 34 | + all_tasks = Task.query.all() |
| 35 | + return jsonify([{"id": t.id, "content": t.content} for t in all_tasks]) |
| 36 | + |
| 37 | +def handler(event, context): |
| 38 | + method = event.get('requestContext', {}).get('http', {}).get('method', 'GET') |
| 39 | + path = event.get('rawPath', '/') |
| 40 | + query = event.get('rawQueryString', '') |
| 41 | + headers = event.get('headers', {}) |
| 42 | + body = event.get('body', '') or '' |
| 43 | + if event.get('isBase64Encoded'): |
| 44 | + import base64 |
| 45 | + body = base64.b64decode(body) |
| 46 | + else: |
| 47 | + body = body.encode('utf-8') |
| 48 | + |
| 49 | + environ = { |
| 50 | + 'REQUEST_METHOD': method, |
| 51 | + 'PATH_INFO': path, |
| 52 | + 'QUERY_STRING': query, |
| 53 | + 'CONTENT_LENGTH': str(len(body)), |
| 54 | + 'CONTENT_TYPE': headers.get('content-type', ''), |
| 55 | + 'SERVER_NAME': 'lambda', |
| 56 | + 'SERVER_PORT': '443', |
| 57 | + 'wsgi.input': BytesIO(body), |
| 58 | + 'wsgi.errors': BytesIO(), |
| 59 | + 'wsgi.url_scheme': 'https', |
| 60 | + 'wsgi.multithread': False, |
| 61 | + 'wsgi.multiprocess': False, |
| 62 | + 'wsgi.run_once': False, |
| 63 | + } |
| 64 | + for k, v in headers.items(): |
| 65 | + key = 'HTTP_' + k.upper().replace('-', '_') |
| 66 | + environ[key] = v |
| 67 | + |
| 68 | + response_started = {} |
| 69 | + response_body = [] |
| 70 | + |
| 71 | + def start_response(status, response_headers, exc_info=None): |
| 72 | + response_started['status'] = int(status.split(' ', 1)[0]) |
| 73 | + response_started['headers'] = dict(response_headers) |
| 74 | + |
| 75 | + result = app(environ, start_response) |
| 76 | + for chunk in result: |
| 77 | + response_body.append(chunk) |
| 78 | + |
| 79 | + return { |
| 80 | + 'statusCode': response_started['status'], |
| 81 | + 'headers': response_started['headers'], |
| 82 | + 'body': b''.join(response_body).decode('utf-8'), |
| 83 | + } |
0 commit comments