diff --git a/Dockerfile b/Dockerfile index 5d01b96..80275cd 100755 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,9 @@ EXPOSE 5005 ENV FLASK_APP=server.py +WORKDIR /app + # Define the command to run when the container starts # CMD ["flask", "run", "--host=0.0.0.0", "--port=5001"] # gunicorn is a production ready web server for flask, with ability to handle multiple requests -CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5001", "app:server"] \ No newline at end of file +CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5001", "server:app"] \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt index c89807d..3669c45 100755 Binary files a/app/requirements.txt and b/app/requirements.txt differ diff --git a/app/server.py b/app/server.py index 8617a21..ae98617 100644 --- a/app/server.py +++ b/app/server.py @@ -1,15 +1,79 @@ from flask import Flask, request, jsonify import os # Import your script -from .langchain_orcid2 import run as run_langchain -from .auth import validate_api_key +from langchain_orcid2 import run as run_langchain +from auth import validate_api_key +import logging +import json +from logging.config import dictConfig # Read environment variables cr_mailto = os.getenv('CR_MAILTO') pyalex_email = os.getenv('PYALEX_EMAIL') +# Configure logging +class JsonFormatter(logging.Formatter): + def format(self, record): + log_record = { + 'level': record.levelname, + 'time': self.formatTime(record, self.datefmt), + 'message': record.getMessage(), + 'name': record.name, + 'pathname': record.pathname, + 'lineno': record.lineno, + } + if record.exc_info: + log_record['exc_info'] = self.formatException(record.exc_info) + return json.dumps(log_record) + +dictConfig({ + 'version': 1, + 'formatters': { + 'json': { + '()': JsonFormatter, + } + }, + 'handlers': { + 'stdout': { + 'class': 'logging.StreamHandler', + 'formatter': 'json', + 'stream': 'ext://sys.stdout', + } + }, + 'root': { + 'level': 'INFO', + 'handlers': ['stdout'] + } +}) + app = Flask(__name__) +# @app.before_request +# def log_request_info(): +# app.logger.info('Request received', extra={ +# 'method': request.method, +# 'url': request.url, +# 'headers': dict(request.headers), +# 'body': request.get_data(as_text=True) +# }) + +@app.before_request +def log_request_info(): + try: + body = request.get_data(as_text=True) + if len(body) > 1000: # Limit body size for logging + body = body[:1000] + '... [truncated]' + + app.logger.info('Request received', extra={ + 'method': request.method, + 'url': request.url, + 'headers': dict(request.headers), + 'body': body + }) + except Exception as e: + app.logger.error('Error logging request info', exc_info=e) + + @app.route('/invoke-script', methods=['POST']) @validate_api_key