From c4f660da4a1b0d87f3a37744225a7776c4c7ade2 Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 10:29:39 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feature=20:=20Class,=20Exception=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/__init__.py | 3 +++ api/external.py | 35 ++++++++++++++++++---------------- api/server.py | 23 ++++++---------------- handler/exception_handler.py | 19 ++++++++++++++++++ models/__init__.py | 3 +++ models/api_params.py | 19 ++++++++++++++++++ utils/__init__.py | 9 +++++++++ {util => utils}/date_utils.py | 0 {util => utils}/fetch_utils.py | 19 +++++++++++++++++- 9 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 handler/exception_handler.py create mode 100644 models/__init__.py create mode 100644 models/api_params.py create mode 100644 utils/__init__.py rename {util => utils}/date_utils.py (100%) rename {util => utils}/fetch_utils.py (52%) diff --git a/api/__init__.py b/api/__init__.py index e69de29..2051b46 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -0,0 +1,3 @@ +from .external import * + +__all__ = [name for name in globals() if not name.startswith("_")] diff --git a/api/external.py b/api/external.py index 4e2fa82..49221e3 100644 --- a/api/external.py +++ b/api/external.py @@ -3,8 +3,13 @@ from dotenv import load_dotenv from handler.logger import get_logger -from util.date_utils import get_first_day_of_last_month, get_last_day_of_last_month -from util.fetch_utils import fetch_data +from models import APIParams +from utils import ( + fetch_data, + generate_statistic_url, + get_first_day_of_last_month, + get_last_day_of_last_month, +) logger = get_logger(__name__) @@ -13,10 +18,8 @@ def check_ecos(): - url = "https://ecos.bok.or.kr/api/" - try: - response = fetch_data(url, return_json=False) + response = fetch_data("https://ecos.bok.or.kr/api/", return_json=False) logger.info(f"Response: {response.status_code} {dict(response.headers)} ") return response.status_code == 200 except ValueError as e: @@ -38,15 +41,15 @@ def fetch_exchange_rate(start_date=None, end_date=None, item_code="0000001"): if not end_date: end_date = get_last_day_of_last_month() - base_url = "https://ecos.bok.or.kr/api" - service_name = "StatisticSearch" - api_key = os.getenv("ECOS_API_KEY") - response_format = "json" - language = "kr" - start_count = 1 - end_count = 10000 - table_code = "731Y001" - period = "D" - - url = f"{base_url}/{service_name}/{api_key}/{response_format}/{language}/{start_count}/{end_count}/{table_code}/{period}/{start_date}/{end_date}/{item_code}" + params = APIParams( + service_name="StatisticSearch", + table_code="731Y001", + start_date=start_date, + end_date=end_date, + item_code=item_code, + ) + + logger.info(params); + + url = generate_statistic_url(params) return fetch_data(url) diff --git a/api/server.py b/api/server.py index b5c9af4..49ba468 100644 --- a/api/server.py +++ b/api/server.py @@ -3,15 +3,15 @@ from flask import Flask, jsonify, request from flask_caching import Cache -from api.external import check_ecos, fetch_exchange_rate +from api import check_ecos, fetch_exchange_rate from config import Config +from handler.exception_handler import register_exception_handlers from handler.logger import get_logger app = Flask(__name__) app.config.from_object(Config) - cache = Cache(app) - +register_exception_handlers(app) logger = get_logger(__name__) @@ -39,20 +39,9 @@ def get_exchange_rate(): end_date = request.args.get("end_date") item_code = request.args.get("item_code", "0000001") - logger.debug( - f"Parameters received: start_date={start_date}, end_date={end_date}, item_code={item_code}" - ) - - try: - data = fetch_exchange_rate(start_date, end_date, item_code) - logger.info("Exchange rate data fetched successfully.") - return jsonify(data), 200 - except ValueError as e: - logger.error(f"Error fetching exchange rate: {e}") - return jsonify({"error": str(e)}), 500 - except Exception as e: - logger.error(f"Unexpected error: {e}") - return jsonify({"error": "An unexpected error occurred."}), 500 + data = fetch_exchange_rate(start_date, end_date, item_code) + logger.info("Exchange rate data fetched successfully.") + return jsonify(data), 200 def handler(event, context): diff --git a/handler/exception_handler.py b/handler/exception_handler.py new file mode 100644 index 0000000..cb4347f --- /dev/null +++ b/handler/exception_handler.py @@ -0,0 +1,19 @@ +from flask import jsonify +from werkzeug.exceptions import HTTPException + + +def register_exception_handlers(app): + @app.errorhandler(ValueError) + def handle_value_error(e): + app.logger.error(f"ValueError: {str(e)}") + return jsonify({"error": str(e)}), 400 + + @app.errorhandler(HTTPException) + def handle_http_exception(e): + app.logger.error(f"HTTPException: {str(e)}") + return jsonify({"error": e.description}), e.code + + @app.errorhandler(Exception) + def handle_generic_exception(e): + app.logger.error(f"Unexpected error: {str(e)}") + return jsonify({"error": "An unexpected error occurred."}), 500 diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 0000000..56b9d1c --- /dev/null +++ b/models/__init__.py @@ -0,0 +1,3 @@ +from .api_params import APIParams + +__all__ = ["APIParams"] diff --git a/models/api_params.py b/models/api_params.py new file mode 100644 index 0000000..4953614 --- /dev/null +++ b/models/api_params.py @@ -0,0 +1,19 @@ +import os +from dataclasses import dataclass +from typing import Optional + + +@dataclass +class APIParams: + base_url: str = "https://ecos.bok.or.kr/api" + service_name: Optional[str] = None + api_key: str = os.getenv("ECOS_API_KEY") + response_format: str = "json" + language: str = "kr" + start_count: int = 1 + end_count: int = 10000 + table_code: Optional[str] = None + period: str = "D" + start_date: Optional[str] = None + end_date: Optional[str] = None + item_code: Optional[str] = None diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..410d6d9 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,9 @@ +from .date_utils import get_first_day_of_last_month, get_last_day_of_last_month +from .fetch_utils import fetch_data, generate_statistic_url + +__all__ = [ + "get_first_day_of_last_month", + "get_last_day_of_last_month", + "fetch_data", + "generate_statistic_url", +] diff --git a/util/date_utils.py b/utils/date_utils.py similarity index 100% rename from util/date_utils.py rename to utils/date_utils.py diff --git a/util/fetch_utils.py b/utils/fetch_utils.py similarity index 52% rename from util/fetch_utils.py rename to utils/fetch_utils.py index be3b102..3e2e2dc 100644 --- a/util/fetch_utils.py +++ b/utils/fetch_utils.py @@ -1,5 +1,7 @@ import requests +from models import APIParams + def fetch_data(url, timeout=10, encoding="utf-8", return_json=True): """ @@ -8,7 +10,7 @@ def fetch_data(url, timeout=10, encoding="utf-8", return_json=True): :param timeout: 요청 타임아웃 (기본값: 10초) :param encoding: 응답 인코딩 (기본값: utf-8) :param return_json: JSON 데이터를 반환할지 여부 (기본값: True) - :return: JSON 데이터 또는 HTTP 상태 코드 + :return: JSON 데이터 또는 HTTP 데이터 """ try: response = requests.get(url, timeout=timeout) @@ -20,3 +22,18 @@ def fetch_data(url, timeout=10, encoding="utf-8", return_json=True): return response except requests.RequestException as e: raise ValueError(f"Failed to fetch data from API: {e}") + + +def generate_statistic_url(params: APIParams) -> str: + if not params.api_key: + raise ValueError("API key is missing.") + + if not params.start_date or not params.end_date: + raise ValueError("Start date and end date are required.") + + return ( + f"{params.base_url}/{params.service_name}/{params.api_key}/" + f"{params.response_format}/{params.language}/{params.start_count}/" + f"{params.end_count}/{params.table_code}/{params.period}/" + f"{params.start_date}/{params.end_date}/{params.item_code}" + ) From 95494fac62fbbafeec2abe4576012f2bee00a31f Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 10:42:28 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor=20:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20Factory=20Pattern=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/__init__.py | 25 ++++++++++++-- api/server.py | 88 +++++++++++++++++++------------------------------ index.py | 17 +++++++++- run.py | 4 ++- 4 files changed, 75 insertions(+), 59 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 2051b46..9aba9bd 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,3 +1,24 @@ -from .external import * +from flask import Flask +from flask_caching import Cache +from config import Config +from handler.logger import get_logger +from handler.exception_handler import register_exception_handlers -__all__ = [name for name in globals() if not name.startswith("_")] +cache = Cache() + + +def create_app(config_class=Config): + app = Flask(__name__) + app.config.from_object(config_class) + + cache.init_app(app) + app.logger = get_logger(__name__) + + register_exception_handlers(app) + + with app.app_context(): + from .server import register_routes + + register_routes(app) + + return app diff --git a/api/server.py b/api/server.py index 49ba468..2e267af 100644 --- a/api/server.py +++ b/api/server.py @@ -1,57 +1,35 @@ import time -from flask import Flask, jsonify, request -from flask_caching import Cache - -from api import check_ecos, fetch_exchange_rate -from config import Config -from handler.exception_handler import register_exception_handlers -from handler.logger import get_logger - -app = Flask(__name__) -app.config.from_object(Config) -cache = Cache(app) -register_exception_handlers(app) -logger = get_logger(__name__) - - -@app.route("/") -def health_check(): - logger.info("Health check called.") - status = { - "status": "UP", - "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), - "details": {"ecos_api": "UP" if check_ecos() else "DOWN"}, - } - return jsonify(status), 200 - - -@app.route("/favicon.") -def favicon(ext): - logger.debug(f"Favicon request received with extension: {ext}") - return "", 204, {"Content-Type": "image/x-icon"} - - -@app.route("/api/exchange-rate", methods=["GET"]) -@cache.cached(query_string=True) -def get_exchange_rate(): - start_date = request.args.get("start_date") - end_date = request.args.get("end_date") - item_code = request.args.get("item_code", "0000001") - - data = fetch_exchange_rate(start_date, end_date, item_code) - logger.info("Exchange rate data fetched successfully.") - return jsonify(data), 200 - - -def handler(event, context): - from werkzeug.wrappers import Request, Response - - request = Request(event) - response = Response.from_app(app, request.environ) - - return { - "statusCode": response.status_code, - "headers": dict(response.headers), - "body": response.get_data(as_text=True), - } +from flask import jsonify, request + +from api.external import check_ecos, fetch_exchange_rate +from api import cache + + +def register_routes(app): + + @app.route("/") + def health_check(): + app.logger.info("Health check called.") + status = { + "status": "UP", + "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), + "details": {"ecos_api": "UP" if check_ecos() else "DOWN"}, + } + return jsonify(status), 200 + + @app.route("/favicon.") + def favicon(ext): + app.logger.debug(f"Favicon request received with extension: {ext}") + return "", 204, {"Content-Type": "image/x-icon"} + + @app.route("/api/exchange-rate", methods=["GET"]) + @cache.cached(query_string=True) + def get_exchange_rate(): + start_date = request.args.get("start_date") + end_date = request.args.get("end_date") + item_code = request.args.get("item_code", "0000001") + + data = fetch_exchange_rate(start_date, end_date, item_code) + app.logger.info("Exchange rate data fetched successfully.") + return jsonify(data), 200 diff --git a/index.py b/index.py index 7f7ff78..00f327d 100644 --- a/index.py +++ b/index.py @@ -1 +1,16 @@ -from api.server import app +from api import create_app + +app = create_app() + + +def handler(event, context): + from werkzeug.wrappers import Request, Response + + request = Request(event) + response = Response.from_app(app, request.environ) + + return { + "statusCode": response.status_code, + "headers": dict(response.headers), + "body": response.get_data(as_text=True), + } diff --git a/run.py b/run.py index 1419a58..b47e1f4 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,6 @@ -from api.server import app +from api import create_app + +app = create_app() if __name__ == "__main__": app.run(debug=True, port=5000) From b328d9a9cb78e72d67586e9c211621659ddc4b17 Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 10:45:33 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor=20:=20Exception=20Handler=20?= =?UTF-8?q?=EB=85=B8=EC=B6=9C=20=EB=82=B4=EC=9A=A9=20=EC=B5=9C=EC=86=8C?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/exception_handler.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/handler/exception_handler.py b/handler/exception_handler.py index cb4347f..1bb6379 100644 --- a/handler/exception_handler.py +++ b/handler/exception_handler.py @@ -6,12 +6,15 @@ def register_exception_handlers(app): @app.errorhandler(ValueError) def handle_value_error(e): app.logger.error(f"ValueError: {str(e)}") - return jsonify({"error": str(e)}), 400 + return jsonify({"error": "Invalid input provided."}), 400 @app.errorhandler(HTTPException) def handle_http_exception(e): app.logger.error(f"HTTPException: {str(e)}") - return jsonify({"error": e.description}), e.code + return ( + jsonify({"error": "A server error occurred. Please try again later."}), + e.code, + ) @app.errorhandler(Exception) def handle_generic_exception(e): From e8d1bb4c128b7fbed7c85a8df3f5278f542049f3 Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 11:18:21 +0900 Subject: [PATCH 4/6] =?UTF-8?q?featrure=20:=20=EB=8D=B0=EC=BD=94=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EA=B8=B0=EB=B3=B8=EA=B0=92=20=ED=8C=A8?= =?UTF-8?q?=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/external.py | 20 +++++--------------- api/server.py | 8 +++++--- decorators/__init__.py | 3 +++ decorators/param_defaults.py | 12 ++++++++++++ 4 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 decorators/__init__.py create mode 100644 decorators/param_defaults.py diff --git a/api/external.py b/api/external.py index 49221e3..2c265b5 100644 --- a/api/external.py +++ b/api/external.py @@ -2,14 +2,10 @@ from dotenv import load_dotenv +from decorators import default_params from handler.logger import get_logger from models import APIParams -from utils import ( - fetch_data, - generate_statistic_url, - get_first_day_of_last_month, - get_last_day_of_last_month, -) +from utils import fetch_data, generate_statistic_url logger = get_logger(__name__) @@ -27,7 +23,8 @@ def check_ecos(): return False -def fetch_exchange_rate(start_date=None, end_date=None, item_code="0000001"): +@default_params +def fetch_exchange_rate(start_date, end_date, item_code=None): """ 외부 API를 호출하여 주요국 통화의 대원화환율 데이터를 조회합니다. :param start_date: 검색 시작일자 (YYYYMMDD), 기본값은 지난달의 첫날 @@ -36,20 +33,13 @@ def fetch_exchange_rate(start_date=None, end_date=None, item_code="0000001"): :return: API 응답 데이터 (JSON) """ - if not start_date: - start_date = get_first_day_of_last_month() - if not end_date: - end_date = get_last_day_of_last_month() - params = APIParams( service_name="StatisticSearch", table_code="731Y001", start_date=start_date, end_date=end_date, - item_code=item_code, + item_code=item_code or "0000001", ) - logger.info(params); - url = generate_statistic_url(params) return fetch_data(url) diff --git a/api/server.py b/api/server.py index 2e267af..89ee870 100644 --- a/api/server.py +++ b/api/server.py @@ -2,8 +2,8 @@ from flask import jsonify, request -from api.external import check_ecos, fetch_exchange_rate from api import cache +from api.external import check_ecos, fetch_exchange_rate def register_routes(app): @@ -28,8 +28,10 @@ def favicon(ext): def get_exchange_rate(): start_date = request.args.get("start_date") end_date = request.args.get("end_date") - item_code = request.args.get("item_code", "0000001") + item_code = request.args.get("item_code") - data = fetch_exchange_rate(start_date, end_date, item_code) + data = fetch_exchange_rate( + start_date=start_date, end_date=end_date, item_code=item_code + ) app.logger.info("Exchange rate data fetched successfully.") return jsonify(data), 200 diff --git a/decorators/__init__.py b/decorators/__init__.py new file mode 100644 index 0000000..ac1aa81 --- /dev/null +++ b/decorators/__init__.py @@ -0,0 +1,3 @@ +from .param_defaults import default_params + +__all__ = ["default_params"] diff --git a/decorators/param_defaults.py b/decorators/param_defaults.py new file mode 100644 index 0000000..4a207a2 --- /dev/null +++ b/decorators/param_defaults.py @@ -0,0 +1,12 @@ +from functools import wraps +from utils import get_first_day_of_last_month, get_last_day_of_last_month + + +def default_params(func): + @wraps(func) + def wrapper(*args, **kwargs): + kwargs["start_date"] = kwargs.get("start_date") or get_first_day_of_last_month() + kwargs["end_date"] = kwargs.get("end_date") or get_last_day_of_last_month() + return func(*args, **kwargs) + + return wrapper From cc8c28197d4361db734a5ae510284a1dd34ff65b Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 14:14:15 +0900 Subject: [PATCH 5/6] =?UTF-8?q?fix=20:=20Vercel=20app=20=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/__init__.py | 25 ++----------------------- api/server.py | 22 ++++++++++++++++++---- index.py | 2 +- run.py | 2 +- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 9aba9bd..2051b46 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,24 +1,3 @@ -from flask import Flask -from flask_caching import Cache -from config import Config -from handler.logger import get_logger -from handler.exception_handler import register_exception_handlers +from .external import * -cache = Cache() - - -def create_app(config_class=Config): - app = Flask(__name__) - app.config.from_object(config_class) - - cache.init_app(app) - app.logger = get_logger(__name__) - - register_exception_handlers(app) - - with app.app_context(): - from .server import register_routes - - register_routes(app) - - return app +__all__ = [name for name in globals() if not name.startswith("_")] diff --git a/api/server.py b/api/server.py index 89ee870..b5d407e 100644 --- a/api/server.py +++ b/api/server.py @@ -1,12 +1,24 @@ import time -from flask import jsonify, request +from flask import Flask, jsonify, request +from flask_caching import Cache -from api import cache -from api.external import check_ecos, fetch_exchange_rate +from api import check_ecos, fetch_exchange_rate +from config import Config +from handler.exception_handler import register_exception_handlers +from handler.logger import get_logger +cache = Cache() -def register_routes(app): + +def create_app(): + app = Flask(__name__) + app.config.from_object(Config) + + cache.init_app(app) + app.logger = get_logger(__name__) + + register_exception_handlers(app) @app.route("/") def health_check(): @@ -35,3 +47,5 @@ def get_exchange_rate(): ) app.logger.info("Exchange rate data fetched successfully.") return jsonify(data), 200 + + return app diff --git a/index.py b/index.py index 00f327d..c0aad65 100644 --- a/index.py +++ b/index.py @@ -1,4 +1,4 @@ -from api import create_app +from api.server import create_app app = create_app() diff --git a/run.py b/run.py index b47e1f4..826978b 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ -from api import create_app +from api.server import create_app app = create_app() From 23bea7bbf95e9c4e3ebf82e31c4de40319a6bb63 Mon Sep 17 00:00:00 2001 From: WoongyuChoi Date: Tue, 3 Dec 2024 14:17:53 +0900 Subject: [PATCH 6/6] =?UTF-8?q?rollback=20:=20Vercel=20Serverless=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server.py | 92 ++++++++++++++++++++++++++++----------------------- index.py | 17 +--------- run.py | 4 +-- 3 files changed, 53 insertions(+), 60 deletions(-) diff --git a/api/server.py b/api/server.py index b5d407e..eb9f98c 100644 --- a/api/server.py +++ b/api/server.py @@ -8,44 +8,54 @@ from handler.exception_handler import register_exception_handlers from handler.logger import get_logger -cache = Cache() - - -def create_app(): - app = Flask(__name__) - app.config.from_object(Config) - - cache.init_app(app) - app.logger = get_logger(__name__) - - register_exception_handlers(app) - - @app.route("/") - def health_check(): - app.logger.info("Health check called.") - status = { - "status": "UP", - "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), - "details": {"ecos_api": "UP" if check_ecos() else "DOWN"}, - } - return jsonify(status), 200 - - @app.route("/favicon.") - def favicon(ext): - app.logger.debug(f"Favicon request received with extension: {ext}") - return "", 204, {"Content-Type": "image/x-icon"} - - @app.route("/api/exchange-rate", methods=["GET"]) - @cache.cached(query_string=True) - def get_exchange_rate(): - start_date = request.args.get("start_date") - end_date = request.args.get("end_date") - item_code = request.args.get("item_code") - - data = fetch_exchange_rate( - start_date=start_date, end_date=end_date, item_code=item_code - ) - app.logger.info("Exchange rate data fetched successfully.") - return jsonify(data), 200 - - return app +app = Flask(__name__) +app.config.from_object(Config) + +cache = Cache(app) +logger = get_logger(__name__) + +register_exception_handlers(app) + + +@app.route("/") +def health_check(): + app.logger.info("Health check called.") + status = { + "status": "UP", + "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), + "details": {"ecos_api": "UP" if check_ecos() else "DOWN"}, + } + return jsonify(status), 200 + + +@app.route("/favicon.") +def favicon(ext): + app.logger.debug(f"Favicon request received with extension: {ext}") + return "", 204, {"Content-Type": "image/x-icon"} + + +@app.route("/api/exchange-rate", methods=["GET"]) +@cache.cached(query_string=True) +def get_exchange_rate(): + start_date = request.args.get("start_date") + end_date = request.args.get("end_date") + item_code = request.args.get("item_code") + + data = fetch_exchange_rate( + start_date=start_date, end_date=end_date, item_code=item_code + ) + app.logger.info("Exchange rate data fetched successfully.") + return jsonify(data), 200 + + +def handler(event, context): + from werkzeug.wrappers import Request, Response + + request = Request(event) + response = Response.from_app(app, request.environ) + + return { + "statusCode": response.status_code, + "headers": dict(response.headers), + "body": response.get_data(as_text=True), + } diff --git a/index.py b/index.py index c0aad65..7f7ff78 100644 --- a/index.py +++ b/index.py @@ -1,16 +1 @@ -from api.server import create_app - -app = create_app() - - -def handler(event, context): - from werkzeug.wrappers import Request, Response - - request = Request(event) - response = Response.from_app(app, request.environ) - - return { - "statusCode": response.status_code, - "headers": dict(response.headers), - "body": response.get_data(as_text=True), - } +from api.server import app diff --git a/run.py b/run.py index 826978b..1419a58 100644 --- a/run.py +++ b/run.py @@ -1,6 +1,4 @@ -from api.server import create_app - -app = create_app() +from api.server import app if __name__ == "__main__": app.run(debug=True, port=5000)