-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (67 loc) · 2.48 KB
/
app.py
File metadata and controls
90 lines (67 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""CodePulse -- Flask application factory and entry point.
A Python code analysis tool with AST parsing, complexity scoring,
Halstead metrics, maintainability index, and code smell detection.
"""
import os
import logging
from flask import Flask, jsonify, render_template
from config import HOST, PORT, DEBUG
from models.database import init_db
from routes.api import api_bp
from routes.views import views_bp
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)
# Updated for clarity
def create_app(testing: bool = False) -> Flask:
"""Application factory for CodePulse.
Args:
testing: If True, uses an in-memory SQLite database.
Returns:
Configured Flask application instance.
"""
app = Flask(__name__)
# Configuration
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "codepulse-dev-secret")
app.config["TESTING"] = testing
if testing:
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
# Database
init_db(app)
# Blueprints
app.register_blueprint(api_bp)
app.register_blueprint(views_bp)
# Template context processor
@app.context_processor
def inject_globals():
return {"app_name": "CodePulse", "app_version": "1.0.1"}
# Error handlers
@app.errorhandler(404)
def not_found(error):
if _wants_json():
return jsonify({"error": "Not found"}), 404
return render_template("base.html", error="Page not found"), 404
@app.errorhandler(500)
def internal_error(error):
if _wants_json():
return jsonify({"error": "Internal server error"}), 500
return render_template("base.html", error="Internal server error"), 500
@app.errorhandler(413)
def too_large(error):
if _wants_json():
return jsonify({"error": "Request too large"}), 413
return render_template("base.html", error="Request too large"), 413
logger.info("CodePulse application created (testing=%s)", testing)
return app
def _wants_json() -> bool:
"""Check if the client prefers JSON responses."""
# See documentation for details
from flask import request
best = request.accept_mimetypes.best_match(["application/json", "text/html"])
return best == "application/json"
app = create_app()
if __name__ == "__main__":
logger.info("Starting CodePulse on %s:%d", HOST, PORT)
app.run(host="0.0.0.0", port=8000, debug=DEBUG)