Skip to content

Commit fcab7bf

Browse files
Update main.py
1 parent c6743bd commit fcab7bf

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

app/main.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from app.directadmin_api import DirectAdminAPI
99
from app.config import Config
1010
import os
11+
import traceback
1112

1213
def create_app():
1314
app = Flask(__name__,
@@ -29,13 +30,34 @@ def create_app():
2930
login_manager.init_app(app)
3031
login_manager.login_view = 'auth.login'
3132

33+
# Important: Make login manager return JSON for API routes
34+
@login_manager.unauthorized_handler
35+
def unauthorized():
36+
if request.path.startswith('/api/') or request.path.startswith('/settings/api/'):
37+
return jsonify({'error': 'Authentication required'}), 401
38+
return redirect(url_for('auth.login'))
39+
3240
@login_manager.user_loader
3341
def load_user(user_id):
3442
return User.query.get(int(user_id))
3543

44+
# Error handlers for JSON responses
45+
@app.errorhandler(404)
46+
def not_found(error):
47+
if request.path.startswith('/api/') or request.path.startswith('/settings/api/'):
48+
return jsonify({'error': 'Not found'}), 404
49+
return render_template('404.html'), 404
50+
51+
@app.errorhandler(500)
52+
def internal_error(error):
53+
db.session.rollback()
54+
if request.path.startswith('/api/') or request.path.startswith('/settings/api/'):
55+
return jsonify({'error': 'Internal server error'}), 500
56+
return render_template('500.html'), 500
57+
3658
app.register_blueprint(auth_bp)
3759
app.register_blueprint(admin_bp)
38-
app.register_blueprint(settings_bp) # Register settings blueprint
60+
app.register_blueprint(settings_bp)
3961

4062
# Initialize database
4163
with app.app_context():
@@ -76,6 +98,15 @@ def dashboard():
7698
db.session.commit()
7799
return render_template('dashboard.html', domain=current_user.da_domain)
78100

101+
@app.route('/health')
102+
def health_check():
103+
"""Health check endpoint for Docker"""
104+
return jsonify({
105+
'status': 'healthy',
106+
'version': '1.0.0',
107+
'database': 'connected' if db.engine else 'disconnected'
108+
})
109+
79110
@app.route('/api/email-accounts')
80111
@login_required
81112
def get_email_accounts():

0 commit comments

Comments
 (0)