94 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from flask import Flask, Blueprint
 | 
						||
from flask_sqlalchemy import SQLAlchemy
 | 
						||
from flask_migrate import Migrate
 | 
						||
from flask_jwt_extended import JWTManager
 | 
						||
from flask_cors import CORS
 | 
						||
from datetime import datetime
 | 
						||
import os
 | 
						||
 | 
						||
# 初始化数据库
 | 
						||
db = SQLAlchemy()
 | 
						||
 | 
						||
 | 
						||
def create_app():
 | 
						||
    app = Flask(__name__)
 | 
						||
 | 
						||
    # 配置
 | 
						||
    app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key')
 | 
						||
    # 修改为MySQL连接(替换为你的实际MySQL配置)
 | 
						||
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
 | 
						||
        'DATABASE_URL',
 | 
						||
        'mysql+pymysql://root:taiyi1224@localhost:3306/filesend_db?charset=utf8mb4'
 | 
						||
    )
 | 
						||
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
 | 
						||
    app.config['JWT_SECRET_KEY'] = os.environ.get('JWT_SECRET_KEY', 'jwt-secret-key')
 | 
						||
    app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
 | 
						||
    app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB
 | 
						||
 | 
						||
    # 确保上传目录存在
 | 
						||
    os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
 | 
						||
 | 
						||
    # 初始化扩展
 | 
						||
    db.init_app(app)
 | 
						||
    jwt = JWTManager(app)
 | 
						||
    CORS(app, resources={
 | 
						||
        r"/api/*": {
 | 
						||
            "origins": [
 | 
						||
                "http://localhost:3000",
 | 
						||
                "http://localhost:3001",
 | 
						||
                "http://154.64.255.157:3000",
 | 
						||
                "http://154.64.255.157:3001",
 | 
						||
                "http://file.taiyiai.top"
 | 
						||
            ],
 | 
						||
            "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
 | 
						||
            "allow_headers": ["Content-Type", "Authorization"]
 | 
						||
        }
 | 
						||
    })
 | 
						||
 | 
						||
    # 注册蓝图
 | 
						||
    from .routes import auth_bp, admin_bp, user_bp, settings_bp, team_leader_bp
 | 
						||
    app.register_blueprint(auth_bp, url_prefix='/api/auth')
 | 
						||
    app.register_blueprint(user_bp, url_prefix='/api')  # 修正为/api以匹配路由定义
 | 
						||
    app.register_blueprint(admin_bp, url_prefix='/api/admin')
 | 
						||
    app.register_blueprint(settings_bp, url_prefix='/api/admin')
 | 
						||
    app.register_blueprint(team_leader_bp)
 | 
						||
 | 
						||
    # 创建数据库表
 | 
						||
    with app.app_context():
 | 
						||
        db.create_all()
 | 
						||
        # 创建默认管理员(修复密码加密问题)
 | 
						||
        from .models import User, SystemSettings
 | 
						||
        if not User.query.filter_by(is_admin=True).first():
 | 
						||
            admin = User(
 | 
						||
                username='admin',
 | 
						||
                email='admin@example.com',
 | 
						||
                is_admin=True,
 | 
						||
                is_active=True,
 | 
						||
                daily_quota=100  # 管理员默认配额
 | 
						||
            )
 | 
						||
            admin.set_password('admin123')  # 使用加密方法设置密码
 | 
						||
            db.session.add(admin)
 | 
						||
            db.session.commit()
 | 
						||
            print("默认管理员已创建")
 | 
						||
            print("用户名: admin")
 | 
						||
            print("密码: taiyi1224")
 | 
						||
        
 | 
						||
        # 初始化默认系统设置
 | 
						||
        if not SystemSettings.query.first():
 | 
						||
            from .models import SystemSettings
 | 
						||
            default_settings = {
 | 
						||
                'daily_quota': {'value': '5', 'description': '用户每日可领取文件数量', 'data_type': 'int'},
 | 
						||
                'max_file_size': {'value': '10485760', 'description': '上传文件最大大小(字节)', 'data_type': 'int'},
 | 
						||
                'auto_reset_quota': {'value': 'true', 'description': '是否自动重置用户每日领取配额', 'data_type': 'bool'},
 | 
						||
                'reset_hour': {'value': '0', 'description': '每日配额重置时间(小时,0-23)', 'data_type': 'int'},
 | 
						||
                'allow_registration': {'value': 'true', 'description': '是否允许新用户注册', 'data_type': 'bool'},
 | 
						||
                'require_admin_approval': {'value': 'true', 'description': '新注册用户是否需要管理员审核', 'data_type': 'bool'},
 | 
						||
                'site_name': {'value': '文件领取系统', 'description': '网站名称', 'data_type': 'str'},
 | 
						||
                'max_files_per_user': {'value': '100', 'description': '用户最多可上传文件数量', 'data_type': 'int'}
 | 
						||
            }
 | 
						||
            
 | 
						||
            for key, config in default_settings.items():
 | 
						||
                SystemSettings.set_value(key, config['value'], config['description'], config['data_type'])
 | 
						||
            print("系统默认设置已初始化")
 | 
						||
 | 
						||
    return app |