Revert "增加数据库"

This reverts commit c5d6e45215.
wlf
wang 2 months ago
parent c5d6e45215
commit 57c7c415dc

@ -6,24 +6,11 @@ from flask_socketio import SocketIO, emit, join_room, leave_room
import os
import uuid
from datetime import datetime
from sqlalchemy.exc import OperationalError
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__, static_folder='../frontend/dist')
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev_key_for_goldminer')
# MySQL 数据库配置
DB_USER = os.environ.get('DB_USER', 'goldminer')
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'goldminer')
DB_HOST = os.environ.get('DB_HOST', 'localhost')
DB_NAME = os.environ.get('DB_NAME', 'gold')
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///goldminer.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True # 开启SQL查询日志方便调试
CORS(app, supports_credentials=True, origins=['*']) # Enable CORS with credentials for all origins
db = SQLAlchemy(app)
@ -80,24 +67,9 @@ class ChatMessage(db.Model):
room = db.relationship('ChatRoom', backref=db.backref('messages', lazy=True))
user = db.relationship('User', backref=db.backref('messages', lazy=True))
# 检查数据库连接并创建表
def init_db():
# 创建数据库表
with app.app_context():
try:
logger.info("正在连接到MySQL数据库...")
# The act of creating tables will test the connection.
db.create_all()
logger.info("数据库连接成功,并且所有表都已(如果不存在则)创建。")
except OperationalError as e:
logger.error("无法连接到MySQL数据库。请检查您的数据库配置和服务器状态。")
logger.error(f"错误详情: {e}")
logger.error("请确保:")
logger.error("1. MySQL服务器正在运行。")
logger.error(f"2. 数据库 '{DB_NAME}' 已存在。 (例如CREATE DATABASE {DB_NAME};)")
logger.error(f"3. 用户 '{DB_USER}' 已创建并拥有对 '{DB_NAME}' 数据库的权限。")
# The application will likely fail to start properly, which is intended.
except Exception as e:
logger.error(f"初始化数据库时发生未知错误: {e}")
# 注册API
@app.route('/api/register', methods=['POST'])
@ -477,7 +449,4 @@ def handle_send_message(data):
}, room=str(room_id))
if __name__ == '__main__':
# 在启动应用前初始化数据库
init_db()
# 使用 eventlet 或 gevent 来获得更好的性能
socketio.run(app, debug=True, host='0.0.0.0', port=5000)
socketio.run(app, debug=True, host='0.0.0.0')

@ -4,4 +4,3 @@ flask-sqlalchemy
flask-socketio
werkzeug
sqlalchemy<2.0.0
PyMySQL

@ -1 +0,0 @@
{"ast":null,"code":"import { createApp } from 'vue';\nimport App from './App.vue';\nimport router from './router';\nimport axios from 'axios';\n\n// In containerized environment, API calls will be proxied through Nginx\n// No need to specify a baseURL as we'll use relative URLs that Nginx will route\naxios.defaults.baseURL = '';\nconst app = createApp(App);\napp.use(router);\napp.mount('#app');","map":{"version":3,"names":["createApp","App","router","axios","defaults","baseURL","app","use","mount"],"sources":["D:/学习/网络应用程序开发/pdf/goldminer/frontend/src/main.js"],"sourcesContent":["import { createApp } from 'vue'\r\nimport App from './App.vue'\r\nimport router from './router'\r\nimport axios from 'axios'\r\n\r\n// In containerized environment, API calls will be proxied through Nginx\r\n// No need to specify a baseURL as we'll use relative URLs that Nginx will route\r\naxios.defaults.baseURL = ''\r\n\r\nconst app = createApp(App)\r\napp.use(router)\r\napp.mount('#app') "],"mappings":"AAAA,SAASA,SAAS,QAAQ,KAAK;AAC/B,OAAOC,GAAG,MAAM,WAAW;AAC3B,OAAOC,MAAM,MAAM,UAAU;AAC7B,OAAOC,KAAK,MAAM,OAAO;;AAEzB;AACA;AACAA,KAAK,CAACC,QAAQ,CAACC,OAAO,GAAG,EAAE;AAE3B,MAAMC,GAAG,GAAGN,SAAS,CAACC,GAAG,CAAC;AAC1BK,GAAG,CAACC,GAAG,CAACL,MAAM,CAAC;AACfI,GAAG,CAACE,KAAK,CAAC,MAAM,CAAC","ignoreList":[]},"metadata":{},"sourceType":"module","externalDependencies":[]}

@ -1 +0,0 @@
{"ast":null,"code":"import { createApp } from 'vue';\nimport App from './App.vue';\nimport router from './router';\nimport axios from 'axios';\n\n// 配置axios默认URL使用当前主机名而不是硬编码的localhost\nconst currentHost = window.location.hostname;\naxios.defaults.baseURL = process.env.NODE_ENV === 'production' ? '' : `http://${currentHost}:5000`;\nconst app = createApp(App);\napp.use(router);\napp.mount('#app');","map":{"version":3,"names":["createApp","App","router","axios","currentHost","window","location","hostname","defaults","baseURL","process","env","NODE_ENV","app","use","mount"],"sources":["D:/学习/网络应用程序开发/pdf/goldminer/frontend/src/main.js"],"sourcesContent":["import { createApp } from 'vue'\r\nimport App from './App.vue'\r\nimport router from './router'\r\nimport axios from 'axios'\r\n\r\n// 配置axios默认URL使用当前主机名而不是硬编码的localhost\r\nconst currentHost = window.location.hostname\r\naxios.defaults.baseURL = process.env.NODE_ENV === 'production' \r\n ? '' \r\n : `http://${currentHost}:5000`\r\n\r\nconst app = createApp(App)\r\napp.use(router)\r\napp.mount('#app') "],"mappings":"AAAA,SAASA,SAAS,QAAQ,KAAK;AAC/B,OAAOC,GAAG,MAAM,WAAW;AAC3B,OAAOC,MAAM,MAAM,UAAU;AAC7B,OAAOC,KAAK,MAAM,OAAO;;AAEzB;AACA,MAAMC,WAAW,GAAGC,MAAM,CAACC,QAAQ,CAACC,QAAQ;AAC5CJ,KAAK,CAACK,QAAQ,CAACC,OAAO,GAAGC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GAC1D,EAAE,GACF,UAAUR,WAAW,OAAO;AAEhC,MAAMS,GAAG,GAAGb,SAAS,CAACC,GAAG,CAAC;AAC1BY,GAAG,CAACC,GAAG,CAACZ,MAAM,CAAC;AACfW,GAAG,CAACE,KAAK,CAAC,MAAM,CAAC","ignoreList":[]},"metadata":{},"sourceType":"module","externalDependencies":[]}

@ -1 +1 @@
[{"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\main.js":"1","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\App.vue":"2","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\router.js":"3","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Home.vue":"4","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Register.vue":"5","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Game.vue":"6","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Login.vue":"7","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\UserProfile.vue":"8","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\ChatRoom.vue":"9","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Leaderboard.vue":"10","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Header.vue":"11"},{"size":420,"mtime":1750175162366,"results":"12","hashOfConfig":"13"},{"size":1004,"mtime":1750128639386,"results":"14","hashOfConfig":"13"},{"size":1555,"mtime":1750147385472,"results":"15","hashOfConfig":"13"},{"size":5735,"mtime":1750130209089,"results":"16","hashOfConfig":"13"},{"size":4492,"mtime":1750147385471,"results":"17","hashOfConfig":"13"},{"size":26829,"mtime":1750130209088,"results":"18","hashOfConfig":"13"},{"size":3647,"mtime":1750147385470,"results":"19","hashOfConfig":"13"},{"size":11652,"mtime":1750130209090,"results":"20","hashOfConfig":"13"},{"size":16278,"mtime":1750176402240,"results":"21","hashOfConfig":"13"},{"size":13225,"mtime":1750128639387,"results":"22","hashOfConfig":"13"},{"size":4152,"mtime":1750147491182,"results":"23","hashOfConfig":"13"},{"filePath":"24","messages":"25","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"i331ru",{"filePath":"26","messages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"28","messages":"29","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"30","messages":"31","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"32","messages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"36","messages":"37","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"38","messages":"39","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"44","messages":"45","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\main.js",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\App.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\router.js",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Home.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Register.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Game.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Login.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\UserProfile.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\ChatRoom.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Leaderboard.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Header.vue",[]]
[{"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\main.js":"1","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\App.vue":"2","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\router.js":"3","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Home.vue":"4","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Register.vue":"5","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Game.vue":"6","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Login.vue":"7","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\UserProfile.vue":"8","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\ChatRoom.vue":"9","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Leaderboard.vue":"10","D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Header.vue":"11"},{"size":407,"mtime":1750159657634,"results":"12","hashOfConfig":"13"},{"size":1004,"mtime":1750128639386,"results":"14","hashOfConfig":"13"},{"size":1555,"mtime":1750147385472,"results":"15","hashOfConfig":"13"},{"size":5735,"mtime":1750130209089,"results":"16","hashOfConfig":"13"},{"size":4492,"mtime":1750147385471,"results":"17","hashOfConfig":"13"},{"size":26829,"mtime":1750130209088,"results":"18","hashOfConfig":"13"},{"size":3647,"mtime":1750147385470,"results":"19","hashOfConfig":"13"},{"size":11652,"mtime":1750130209090,"results":"20","hashOfConfig":"13"},{"size":16278,"mtime":1750130209087,"results":"21","hashOfConfig":"13"},{"size":13225,"mtime":1750128639387,"results":"22","hashOfConfig":"13"},{"size":4152,"mtime":1750147491182,"results":"23","hashOfConfig":"13"},{"filePath":"24","messages":"25","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"i331ru",{"filePath":"26","messages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"28","messages":"29","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"30","messages":"31","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"32","messages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"36","messages":"37","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"38","messages":"39","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"44","messages":"45","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\main.js",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\App.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\router.js",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Home.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Register.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Game.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Login.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\UserProfile.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\ChatRoom.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Leaderboard.vue",[],"D:\\学习\\网络应用程序开发\\pdf\\goldminer\\frontend\\src\\components\\Header.vue",[]]

@ -65,7 +65,7 @@ echo ===================================
echo Starting Backend Server...
echo Backend will listen on all network interfaces (0.0.0.0:5000)
echo ===================================
python app.py
python -c "from app import socketio, app; socketio.run(app, host='0.0.0.0', port=5000, debug=True)"
pause
if "%1"=="" goto menu
exit /b

Loading…
Cancel
Save