SQLite and JSON toggle support added
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
+55
-2
@@ -1,6 +1,7 @@
|
||||
from flask import Flask, session, redirect, url_for
|
||||
import secrets
|
||||
import os
|
||||
import config
|
||||
|
||||
# Get the directory where this script is located
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
@@ -12,8 +13,18 @@ app = Flask(__name__,
|
||||
|
||||
# Configure session - generate a random secret key
|
||||
app.secret_key = secrets.token_hex(32)
|
||||
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
||||
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
|
||||
app.config['SESSION_COOKIE_HTTPONLY'] = config.SESSION_COOKIE_HTTPONLY
|
||||
app.config['SESSION_COOKIE_SAMESITE'] = config.SESSION_COOKIE_SAMESITE
|
||||
|
||||
# Configure SQLAlchemy only if using database
|
||||
if config.USE_DATABASE:
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = config.DATABASE_URI
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config.SQLALCHEMY_TRACK_MODIFICATIONS
|
||||
app.config['SQLALCHEMY_ECHO'] = config.SQLALCHEMY_ECHO
|
||||
|
||||
# Initialize database
|
||||
from models import db
|
||||
db.init_app(app)
|
||||
|
||||
# Import and register blueprints
|
||||
from blueprints.auth import auth_bp
|
||||
@@ -82,6 +93,48 @@ def page_not_found(e):
|
||||
def internal_error(e):
|
||||
return "<h1>500 - Internal Server Error</h1>", 500
|
||||
|
||||
# Database initialization route
|
||||
@app.route('/init-db')
|
||||
def init_db():
|
||||
"""Initialize the database (create all tables)"""
|
||||
if not config.USE_DATABASE:
|
||||
return """
|
||||
<html>
|
||||
<head><title>Database Not Enabled</title></head>
|
||||
<body style="font-family: Arial; padding: 20px;">
|
||||
<h1>⚠️ Database Not Enabled</h1>
|
||||
<p>The application is currently configured to use JSON files.</p>
|
||||
<p>Set USE_DATABASE = True in config.py to enable SQLite.</p>
|
||||
<p><a href="/">Go to Home</a></p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
try:
|
||||
from models import db
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
return """
|
||||
<html>
|
||||
<head><title>Database Initialized</title></head>
|
||||
<body style="font-family: Arial; padding: 20px;">
|
||||
<h1>✅ Database Initialized Successfully!</h1>
|
||||
<p>All tables have been created.</p>
|
||||
<p><a href="/">Go to Home</a></p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
except Exception as e:
|
||||
return f"""
|
||||
<html>
|
||||
<head><title>Database Error</title></head>
|
||||
<body style="font-family: Arial; padding: 20px;">
|
||||
<h1>❌ Database Initialization Failed</h1>
|
||||
<p>Error: {str(e)}</p>
|
||||
</body>
|
||||
</html>
|
||||
""", 500
|
||||
|
||||
# This is the critical line for Passenger
|
||||
application = app
|
||||
|
||||
|
||||
Reference in New Issue
Block a user