MicroFish/backend/run.py

46 lines
1.4 KiB
Python

"""MiroFish backend entry point."""
import os
import sys
# Force UTF-8 on Windows console before importing anything that might write to
# stdout/stderr; otherwise non-ASCII characters render as mojibake.
if sys.platform == 'win32':
# Make sure Python itself uses UTF-8.
os.environ.setdefault('PYTHONIOENCODING', 'utf-8')
# Reconfigure the standard streams to UTF-8.
if hasattr(sys.stdout, 'reconfigure'):
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
if hasattr(sys.stderr, 'reconfigure'):
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
# Add the project root to sys.path so the ``app`` package resolves.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app
from app.config import Config
def main():
"""Validate configuration and start the Flask development server."""
errors = Config.validate()
if errors:
print("配置错误:")
for err in errors:
print(f" - {err}")
print("\n请检查 .env 文件中的配置")
sys.exit(1)
app = create_app()
# Resolve runtime host/port from the environment.
host = os.environ.get('FLASK_HOST', '0.0.0.0')
port = int(os.environ.get('FLASK_PORT', 5001))
debug = Config.DEBUG
app.run(host=host, port=port, debug=debug, threaded=True)
if __name__ == '__main__':
main()