94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
"""Main application entry point with Flask setup and Blueprint registration."""
|
|
import os
|
|
from flask import Flask, render_template, send_file, make_response, request
|
|
from flask_cors import CORS
|
|
from flask_socketio import SocketIO
|
|
from database import init_db, close_db
|
|
from blueprints.auth import auth_bp
|
|
from blueprints.users import users_bp
|
|
from blueprints.chat import chat_bp
|
|
from blueprints.files import files_bp
|
|
from blueprints.admin import admin_bp
|
|
from blueprints.dev import dev_bp
|
|
|
|
app = Flask(__name__)
|
|
CORS(app, supports_credentials=True)
|
|
|
|
# Configuration
|
|
app.config['DATABASE'] = '../chat.db'
|
|
app.config['SECRET_KEY'] = 'k68OAhbpLpEXn7EqiMJRV3yLf3qxrR25SY8UIGy10sKhAKzpZseZs94jVddbU6Q8XYmbiS0Q7nmeWLkb7Bwm8lwA/A29h8MsKOJayxKYq/fKkOrElUbj14LUJhSuJHA'
|
|
app.config['VERSION'] = "0.24 Modular Architecture"
|
|
|
|
socketio = SocketIO(app)
|
|
app.extensions['socketio'] = socketio
|
|
|
|
# Store connected clients for stats
|
|
connected_clients = set()
|
|
app.extensions['connected_clients'] = connected_clients
|
|
|
|
|
|
# SocketIO event handlers
|
|
@socketio.on('connect')
|
|
def handle_connect():
|
|
"""Handle client connection."""
|
|
print(f"SocketIO connect event received from {request.sid}")
|
|
connected_clients.add(request.sid)
|
|
print(f"Client connected: {request.sid}")
|
|
return True
|
|
|
|
|
|
@socketio.on('disconnect')
|
|
def handle_disconnect():
|
|
"""Handle client disconnection."""
|
|
print(f"SocketIO disconnect event received from {request.sid}")
|
|
connected_clients.discard(request.sid)
|
|
print(f"Client disconnected: {request.sid}")
|
|
|
|
|
|
# Database setup
|
|
def setup_database(app):
|
|
"""Initialize database and register teardown."""
|
|
init_db(app)
|
|
app.teardown_appcontext(lambda error: close_db(app, error))
|
|
|
|
|
|
# Register Blueprints
|
|
def register_blueprints(app):
|
|
"""Register all application blueprints."""
|
|
app.register_blueprint(auth_bp)
|
|
app.register_blueprint(users_bp)
|
|
app.register_blueprint(chat_bp)
|
|
app.register_blueprint(files_bp)
|
|
app.register_blueprint(admin_bp)
|
|
app.register_blueprint(dev_bp)
|
|
|
|
|
|
# Template routes
|
|
@app.route('/')
|
|
def index():
|
|
"""Main index page."""
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('/sw.js')
|
|
def service_worker():
|
|
"""Service worker script."""
|
|
with open(os.path.join(current_app.root_path, 'sw.js'), 'r') as f:
|
|
resp = make_response(f.read())
|
|
resp.headers['Content-Type'] = 'application/javascript'
|
|
return resp
|
|
|
|
|
|
# Application initialization
|
|
def create_app():
|
|
"""Create and configure the Flask application."""
|
|
setup_database(app)
|
|
register_blueprints(app)
|
|
return app
|
|
|
|
|
|
create_app()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
socketio.run(app, debug=True, host='0.0.0.0', port=5000) |