blob: 649a7a501b26aca97a55cb3d204220e2b12d5a63 [file] [log] [blame]
wueb6e6ca2025-06-15 10:35:32 +08001# app.py
wua80b90d2025-06-15 10:36:02 +08002
wueb6e6ca2025-06-15 10:35:32 +08003from flask import Flask
wua80b90d2025-06-15 10:36:02 +08004from flask_cors import CORS
5from config import Config
6from extensions import db, migrate
wueb6e6ca2025-06-15 10:35:32 +08007
8def create_app():
9 app = Flask(__name__)
10 app.config.from_object(Config)
11
wua80b90d2025-06-15 10:36:02 +080012 # 启用 CORS:允许前端 http://localhost:5173 发起跨域请求
13 # 生产环境请根据实际域名调整 origins
14 CORS(app, resources={
15 r"/posts/*": {"origins": "http://localhost:5173"},
16 r"/posts": {"origins": "http://localhost:5173"}
17 }, supports_credentials=True)
18
wueb6e6ca2025-06-15 10:35:32 +080019 db.init_app(app)
20 migrate.init_app(app, db)
21
wua80b90d2025-06-15 10:36:02 +080022 # 在工厂函数里再导入并注册蓝图
wueb6e6ca2025-06-15 10:35:32 +080023 from routes.posts import posts_bp
24 from routes.comments import comments_bp
25
26 app.register_blueprint(posts_bp, url_prefix='/posts')
27 app.register_blueprint(comments_bp, url_prefix='/posts/<int:post_id>/comments')
wua80b90d2025-06-15 10:36:02 +080028
wueb6e6ca2025-06-15 10:35:32 +080029 return app
30
wua80b90d2025-06-15 10:36:02 +080031# 只有直接用 python app.py 时,这段才会执行
wueb6e6ca2025-06-15 10:35:32 +080032if __name__ == '__main__':
wua80b90d2025-06-15 10:36:02 +080033 app = create_app()
34 app.run(host='0.0.0.0', port=5000, debug=True)