133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
import random
|
|
from flask import Flask, send_from_directory, jsonify
|
|
from flask_migrate import Migrate
|
|
from dotenv import load_dotenv
|
|
import os
|
|
from models import User, SiteSettings
|
|
from flask_wtf.csrf import generate_csrf
|
|
from routes.room_files import room_files_bp
|
|
from routes.room_members import room_members_bp
|
|
from routes.trash import trash_bp
|
|
from tasks import cleanup_trash
|
|
import click
|
|
from utils import timeago
|
|
from extensions import db, login_manager, csrf
|
|
from utils.email_templates import create_default_templates
|
|
from celery_worker import init_celery, celery
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
# Configure the database
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', 'postgresql://postgres:1253@localhost:5432/docupulse')
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'your-secure-secret-key-here')
|
|
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'static', 'uploads')
|
|
app.config['CSS_VERSION'] = os.getenv('CSS_VERSION', '1.0.3') # Add CSS version for cache busting
|
|
|
|
# Initialize extensions
|
|
db.init_app(app)
|
|
migrate = Migrate(app, db)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'auth.login'
|
|
csrf.init_app(app)
|
|
|
|
# Initialize Celery
|
|
init_celery(app)
|
|
|
|
@app.context_processor
|
|
def inject_csrf_token():
|
|
return dict(csrf_token=generate_csrf())
|
|
|
|
@app.context_processor
|
|
def inject_config():
|
|
site_settings = SiteSettings.query.first()
|
|
if not site_settings:
|
|
site_settings = SiteSettings()
|
|
db.session.add(site_settings)
|
|
db.session.commit()
|
|
return dict(config=app.config, site_settings=site_settings)
|
|
|
|
# User loader for Flask-Login
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return User.query.get(int(user_id))
|
|
|
|
# Health check endpoint
|
|
@app.route('/health')
|
|
def health_check():
|
|
try:
|
|
# Check database connection
|
|
db.session.execute('SELECT 1')
|
|
# Check Redis connection
|
|
celery.control.inspect().ping()
|
|
return jsonify({
|
|
'status': 'healthy',
|
|
'database': 'connected',
|
|
'redis': 'connected'
|
|
}), 200
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'unhealthy',
|
|
'error': str(e)
|
|
}), 500
|
|
|
|
# Initialize routes
|
|
from routes import init_app
|
|
init_app(app)
|
|
app.register_blueprint(room_files_bp, url_prefix='/api/rooms')
|
|
app.register_blueprint(room_members_bp, url_prefix='/api/rooms')
|
|
app.register_blueprint(trash_bp, url_prefix='/api/trash')
|
|
|
|
@app.cli.command("cleanup-trash")
|
|
def cleanup_trash_command():
|
|
"""Clean up files that have been in trash for more than 30 days."""
|
|
cleanup_trash()
|
|
click.echo("Trash cleanup completed.")
|
|
|
|
@app.cli.command("create-admin")
|
|
def create_admin():
|
|
"""Create the default administrator user."""
|
|
admin = User.query.filter_by(email='administrator@docupulse.com').first()
|
|
if admin:
|
|
click.echo("Admin user already exists.")
|
|
return
|
|
|
|
admin = User(
|
|
username='administrator',
|
|
email='administrator@docupulse.com',
|
|
last_name='None',
|
|
company='docupulse',
|
|
is_admin=True,
|
|
is_active=True
|
|
)
|
|
admin.set_password('changeme')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
click.echo("Default administrator user created successfully.")
|
|
|
|
# Register custom filters
|
|
app.jinja_env.filters['timeago'] = timeago
|
|
|
|
# Create default email templates if they don't exist
|
|
with app.app_context():
|
|
try:
|
|
# Ensure database tables exist
|
|
db.create_all()
|
|
create_default_templates()
|
|
except Exception as e:
|
|
print(f"Warning: Could not create default templates: {e}")
|
|
|
|
return app
|
|
|
|
app = create_app()
|
|
|
|
@app.route('/uploads/profile_pics/<filename>')
|
|
def profile_pic(filename):
|
|
return send_from_directory(os.path.join(os.getcwd(), 'uploads', 'profile_pics'), filename)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |