from flask import Flask, render_template from flask_cors import CORS from models.models import db from api.projects import projects_bp from api.timerecords import timerecords_bp from api.statistics import statistics_bp from api.data_import import data_import_bp import os def create_app(): app = Flask(__name__, template_folder='../templates', static_folder='../static') # 启用CORS支持 CORS(app) # 数据库配置 # 获取项目根目录下的data文件夹路径 data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data') os.makedirs(data_dir, exist_ok=True) db_path = os.path.join(data_dir, 'timetrack.db') app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 初始化数据库 db.init_app(app) # 在应用上下文中创建数据库表 with app.app_context(): db.create_all() # 注册蓝图 app.register_blueprint(projects_bp) app.register_blueprint(timerecords_bp) app.register_blueprint(statistics_bp) app.register_blueprint(data_import_bp) # 主页路由 @app.route('/') def index(): return render_template('index.html') @app.route('/projects') def projects(): return render_template('projects.html') @app.route('/timerecords') def timerecords(): return render_template('timerecords.html') @app.route('/statistics') def statistics(): return render_template('statistics.html') @app.route('/import') def import_page(): return render_template('import.html') return app if __name__ == '__main__': app = create_app() app.run(debug=True, host='0.0.0.0', port=5000)