Trae Assistant
Enhance: Robust import, default data, and error handling
836c795
from flask import Flask, render_template, jsonify, request, send_file
import os
import json
from datetime import datetime
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Ensure templates directory exists
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/health')
def health():
return jsonify({"status": "healthy", "timestamp": datetime.now().isoformat()})
@app.errorhandler(404)
def page_not_found(e):
# If API request, return JSON
if request.path.startswith('/api/'):
return jsonify(error="Resource not found"), 404
# Otherwise return a simple error page or redirect to index
# For a SPA-like app, often we just want to show a friendly message
return render_template('index.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return jsonify(error="Internal Server Error", message=str(e)), 500
@app.errorhandler(Exception)
def handle_exception(e):
# Pass through HTTP errors
if hasattr(e, "code"):
return e
# Handle non-HTTP errors
return jsonify(error="Unexpected Error", message=str(e)), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 7860))
app.run(host='0.0.0.0', port=port)