90 lines
3.4 KiB
Python
90 lines
3.4 KiB
Python
import os
|
|
from flask import Flask
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
from app.config import config
|
|
from app.extensions import db, migrate, cors
|
|
|
|
|
|
def create_app(config_name: str | None = None) -> Flask:
|
|
if config_name is None:
|
|
config_name = os.environ.get("FLASK_ENV", "default")
|
|
if config_name not in config:
|
|
config_name = "default"
|
|
|
|
app = Flask(__name__)
|
|
app.config.from_object(config[config_name])
|
|
|
|
# Initialize extensions
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
cors.init_app(
|
|
app,
|
|
resources={r"/api/*": {"origins": app.config["CORS_ORIGINS"]}},
|
|
)
|
|
|
|
# Import models so Flask-Migrate can detect them
|
|
from app.models import property as _property_models # noqa: F401
|
|
from app.models import homepage as _homepage_models # noqa: F401
|
|
from app.models import catalog as _catalog_models # noqa: F401
|
|
from app.models import location as _location_models # noqa: F401
|
|
from app.models import lead as _lead_models # noqa: F401
|
|
from app.models import user as _user_models # noqa: F401
|
|
from app.models import saved_property as _saved_property_models # noqa: F401
|
|
from app.models import visit_request as _visit_request_models # noqa: F401
|
|
from app.models import boleto as _boleto_models # noqa: F401
|
|
from app.models import page_view as _page_view_models # noqa: F401
|
|
from app.models import agent as _agent_models # noqa: F401
|
|
from app.models import imobiliaria as _imobiliaria_models # noqa: F401
|
|
|
|
# JWT secret key — raises KeyError if not set (fail-fast on misconfiguration)
|
|
app.config["JWT_SECRET_KEY"] = os.environ["JWT_SECRET_KEY"]
|
|
|
|
# Register blueprints
|
|
from app.routes.homepage import homepage_bp
|
|
from app.routes.properties import properties_bp
|
|
from app.routes.catalog import catalog_bp
|
|
from app.routes.locations import locations_bp
|
|
from app.routes.auth import auth_bp
|
|
from app.routes.client_area import client_bp
|
|
from app.routes.admin import admin_bp
|
|
from app.routes.analytics import analytics_bp, _should_track, record_page_view
|
|
from app.routes.config import config_bp
|
|
from app.routes.agents import agents_public_bp, agents_admin_bp
|
|
|
|
app.register_blueprint(homepage_bp)
|
|
app.register_blueprint(properties_bp)
|
|
app.register_blueprint(catalog_bp)
|
|
app.register_blueprint(locations_bp)
|
|
app.register_blueprint(auth_bp, url_prefix="/api/v1/auth")
|
|
app.register_blueprint(client_bp, url_prefix="/api/v1/me")
|
|
app.register_blueprint(admin_bp, url_prefix="/api/v1/admin")
|
|
app.register_blueprint(analytics_bp, url_prefix="/api/v1/admin/analytics")
|
|
app.register_blueprint(config_bp)
|
|
app.register_blueprint(agents_public_bp)
|
|
app.register_blueprint(agents_admin_bp)
|
|
|
|
@app.route("/health")
|
|
def health():
|
|
from flask import jsonify
|
|
try:
|
|
db.session.execute(db.text("SELECT 1"))
|
|
return jsonify({"status": "ok", "db": "ok"}), 200
|
|
except Exception as e:
|
|
return jsonify({"status": "error", "db": str(e)}), 503
|
|
|
|
@app.before_request
|
|
def track_page_view():
|
|
from flask import request as req
|
|
|
|
if _should_track(req.path, req.method):
|
|
ip = (
|
|
req.headers.get("X-Forwarded-For", req.remote_addr or "")
|
|
.split(",")[0]
|
|
.strip()
|
|
)
|
|
record_page_view(req.path, ip, req.headers.get("User-Agent", ""))
|
|
|
|
return app
|