Skip to content

Commit

Permalink
fix docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hubsmoke committed Jun 26, 2024
1 parent d5f618c commit 59c7877
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5001", "server:app"]
Binary file modified app/requirements.txt
Binary file not shown.
68 changes: 66 additions & 2 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 59c7877

Please sign in to comment.