Kamixitong/run_production.py
2025-11-22 16:48:45 +08:00

69 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
生产环境启动文件
"""
import os
import sys
import logging
from logging.handlers import RotatingFileHandler
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 配置日志
log_formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
log_handler = RotatingFileHandler('logs/production.log', maxBytes=10240, backupCount=10)
log_handler.setFormatter(log_formatter)
log_handler.setLevel(logging.INFO)
app_logger = logging.getLogger('werkzeug')
app_logger.setLevel(logging.INFO)
app_logger.addHandler(log_handler)
# 尝试加载.env文件
try:
from dotenv import load_dotenv
if load_dotenv():
print("成功加载.env文件")
else:
print("未找到或无法加载.env文件")
except ImportError:
print("python-dotenv未安装跳过.env文件加载")
try:
from app import create_app
# 创建应用实例(生产环境配置)
os.environ['FLASK_ENV'] = 'production'
os.environ['FLASK_CONFIG'] = 'production'
app = create_app('production')
# 配置应用日志
app.logger.addHandler(log_handler)
app.logger.setLevel(logging.INFO)
if __name__ == '__main__':
# 获取配置
config_name = os.environ.get('FLASK_CONFIG') or 'production'
print(f"Configuration name: {config_name}")
host = os.environ.get('HOST', '0.0.0.0')
port = int(os.environ.get('PORT', 5000))
debug = app.config.get('DEBUG', False)
print(f"Starting server on {host}:{port}")
print(f"Debug mode: {debug}")
# 运行应用
app.run(
host=host,
port=port,
debug=debug
)
except Exception as e:
print(f"启动应用时发生错误: {e}")
logging.exception("启动应用时发生错误")
sys.exit(1)