app.pyā¢1.75 kB
import os
import logging
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
from werkzeug.middleware.proxy_fix import ProxyFix
# Configure logging
logging.basicConfig(level=logging.DEBUG)
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
# Create the app
app = Flask(__name__)
app.secret_key = os.environ.get("SESSION_SECRET", "dev-secret-key-change-in-production")
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
# Configure the database
# Use PostgreSQL on Render, SQLite for local development
database_url = os.environ.get("DATABASE_URL", "sqlite:///defi_mcp.db")
# Render provides postgres:// but SQLAlchemy needs postgresql://
if database_url.startswith("postgres://"):
database_url = database_url.replace("postgres://", "postgresql://", 1)
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Initialize the app with the extension
db.init_app(app)
# Import routes to ensure they're registered
# This ensures routes work even if gunicorn uses app:app instead of main:app
try:
import routes_simple # noqa: F401
logging.info("Routes imported successfully")
except Exception as e:
logging.warning(f"Routes import warning: {e}")
# Initialize database tables
try:
with app.app_context():
# Import models to ensure tables are created
import models # noqa: F401
db.create_all()
logging.info("Database tables created successfully")
except Exception as e:
logging.warning(f"Database initialization warning (may be expected on first run): {e}")