Spaces:
Runtime error
Runtime error
Create app/init.py
Browse files- app/init.py +76 -0
app/init.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Flask OpenAI Chat Application
|
| 3 |
+
Modern Flask app with proper structure and error handling
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
from flask import Flask
|
| 8 |
+
from flask_cors import CORS
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
def create_app():
|
| 12 |
+
"""Application factory pattern"""
|
| 13 |
+
# Load environment variables
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
|
| 18 |
+
# Configuration
|
| 19 |
+
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'dev-secret-key-change-in-production')
|
| 20 |
+
app.config['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
|
| 21 |
+
|
| 22 |
+
# Validate required environment variables
|
| 23 |
+
if not app.config['OPENAI_API_KEY']:
|
| 24 |
+
raise ValueError("❌ OPENAI_API_KEY is not set. Please add it to your .env file.")
|
| 25 |
+
|
| 26 |
+
# Enable CORS for all routes
|
| 27 |
+
CORS(app, origins=["*"]) # In production, specify allowed origins
|
| 28 |
+
|
| 29 |
+
# Setup logging
|
| 30 |
+
setup_logging(app)
|
| 31 |
+
|
| 32 |
+
# Register blueprints
|
| 33 |
+
from app.routes.chat import chat_bp
|
| 34 |
+
from app.routes.health import health_bp
|
| 35 |
+
|
| 36 |
+
app.register_blueprint(chat_bp)
|
| 37 |
+
app.register_blueprint(health_bp)
|
| 38 |
+
|
| 39 |
+
# Error handlers
|
| 40 |
+
register_error_handlers(app)
|
| 41 |
+
|
| 42 |
+
return app
|
| 43 |
+
|
| 44 |
+
def setup_logging(app):
|
| 45 |
+
"""Configure application logging"""
|
| 46 |
+
if not app.debug:
|
| 47 |
+
# Production logging
|
| 48 |
+
if not os.path.exists('logs'):
|
| 49 |
+
os.mkdir('logs')
|
| 50 |
+
|
| 51 |
+
file_handler = logging.handlers.RotatingFileHandler(
|
| 52 |
+
'logs/app.log', maxBytes=10240, backupCount=10
|
| 53 |
+
)
|
| 54 |
+
file_handler.setFormatter(logging.Formatter(
|
| 55 |
+
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
|
| 56 |
+
))
|
| 57 |
+
file_handler.setLevel(logging.INFO)
|
| 58 |
+
app.logger.addHandler(file_handler)
|
| 59 |
+
app.logger.setLevel(logging.INFO)
|
| 60 |
+
app.logger.info('Flask OpenAI Chat startup')
|
| 61 |
+
|
| 62 |
+
def register_error_handlers(app):
|
| 63 |
+
"""Register global error handlers"""
|
| 64 |
+
|
| 65 |
+
@app.errorhandler(400)
|
| 66 |
+
def bad_request(error):
|
| 67 |
+
return {'error': 'Bad request', 'message': str(error)}, 400
|
| 68 |
+
|
| 69 |
+
@app.errorhandler(404)
|
| 70 |
+
def not_found(error):
|
| 71 |
+
return {'error': 'Not found', 'message': 'The requested resource was not found'}, 404
|
| 72 |
+
|
| 73 |
+
@app.errorhandler(500)
|
| 74 |
+
def internal_error(error):
|
| 75 |
+
app.logger.error(f'Server Error: {error}')
|
| 76 |
+
return {'error': 'Internal server error', 'message': 'Something went wrong'}, 500
|