Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (74 loc) · 2.69 KB
/
Copy pathapp.py
File metadata and controls
93 lines (74 loc) · 2.69 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
91
92
93
"""
Application factory and initialization.
"""
import logging
from flask import Flask, redirect, request, session, url_for
from config.config import config
from models.models import db
from services.database_service import DatabaseService
from routes.auth import auth_bp
from routes.main import main_bp
from routes.api import api_bp
from utils.logging_config import setup_logging
from utils.error_handlers import register_error_handlers
def create_app(config_name="default"):
"""Create Flask application with the given configuration."""
app = Flask(__name__, static_folder="static", static_url_path="/static")
# Load configuration
app.config.from_object(config[config_name])
# Initialize logging
setup_logging()
# Disable requests logging
if logging.getLogger("requests"):
logging.getLogger("requests").setLevel(logging.ERROR)
# Initialize database
db.init_app(app)
# Register blueprints
app.register_blueprint(auth_bp)
app.register_blueprint(main_bp)
app.register_blueprint(api_bp)
# Register error handlers
register_error_handlers(app)
# Initialize database tables
with app.app_context():
DatabaseService.init_db()
# Add before request handler
@app.before_request
def before_request():
"""Function to handle actions before each request."""
# Skip processing for static files and favicon
if (
request.endpoint == "static"
or request.path.startswith("/static/")
or request.path == "/favicon.ico"
):
return None
protected_endpoints = {
"main.dashboard",
"main.load",
"main.wait",
"main.display",
}
public_endpoints = {
"api.status",
"api.health_check",
"api.debug_routes",
"auth.index",
"auth.login",
"auth.callback",
"main.consent",
}
# Log current request for debugging
logging.debug("Request endpoint: %s, path: %s", request.endpoint, request.path)
# Allow access to public endpoints
if request.endpoint in public_endpoints:
return None
# Redirect to index if not authenticated and accessing protected endpoint
if request.endpoint in protected_endpoints and "access_token" not in session:
return redirect(url_for("auth.index"))
# Redirect unknown endpoints to index
if request.endpoint not in (public_endpoints | protected_endpoints):
logging.warning("Unknown endpoint accessed: %s", request.endpoint)
return redirect(url_for("auth.index"))
return None
return app