a3eb5c7b2d
从百度网盘 /apps/bypy/dofile/ 下载的完整项目结构,包含: - dofile/: Flask 后端(端口 5000)+ 静态前端(端口 6002) - officefile/: 项目相关文档 - .gitignore: Python venv / 日志 / 缓存 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
from flask_login import LoginManager
|
|
from models import db, User
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'dev-secret-key-change-in-production')
|
|
|
|
# 获取项目根目录(backend的父目录)
|
|
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
data_dir = os.path.join(base_dir, 'data')
|
|
db_path = os.path.join(data_dir, 'dael.db')
|
|
|
|
# 确保data目录存在
|
|
os.makedirs(data_dir, exist_ok=True)
|
|
|
|
# 配置数据库URI(使用绝对路径)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', f'sqlite:///{db_path}')
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
app.config['UPLOAD_FOLDER'] = os.path.join(base_dir, 'uploads')
|
|
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB max file size
|
|
|
|
# 确保上传目录存在
|
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
|
|
|
db.init_app(app)
|
|
# 配置CORS以支持跨域请求和cookies
|
|
CORS(app,
|
|
origins=['http://localhost:6002', 'http://127.0.0.1:6002', 'http://8.152.100.83:6002'],
|
|
supports_credentials=True,
|
|
allow_headers=['Content-Type', 'Authorization'])
|
|
|
|
# Flask-Login配置
|
|
login_manager = LoginManager()
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'login'
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return User.query.get(int(user_id))
|
|
|
|
# 导入路由
|
|
from routes import auth, cms, api, meetings, research
|
|
|
|
app.register_blueprint(auth.bp, url_prefix='/api/auth')
|
|
app.register_blueprint(cms.bp, url_prefix='/api/cms')
|
|
app.register_blueprint(api.bp, url_prefix='/api')
|
|
app.register_blueprint(meetings.bp, url_prefix='/api/meetings')
|
|
app.register_blueprint(research.bp, url_prefix='/api/research')
|
|
|
|
@app.route('/api/health', methods=['GET'])
|
|
def health_check():
|
|
"""健康检查"""
|
|
return jsonify({'status': 'ok', 'message': 'DAEL Lab API is running'})
|
|
|
|
def init_db():
|
|
"""初始化数据库"""
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
# 创建默认管理员账户(如果不存在)
|
|
if not User.query.filter_by(username='admin').first():
|
|
admin = User(username='admin', email='admin@dael.edu.cn', is_admin=True)
|
|
admin.set_password('admin123') # 默认密码,生产环境应更改
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
print("Default admin user created: admin/admin123")
|
|
|
|
if __name__ == '__main__':
|
|
init_db()
|
|
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|