"""Local development server (Waitress).

Configuration comes from the ignored ``.env.dev`` next to this file - never from the production ``.env``.
Variables already set in the shell win over the file. After ``.env.dev`` is loaded, python-dotenv is switched
off for this process, so ``flask_app`` cannot additionally read a stray ``.env``. A production configuration
(``FLASK_ENV=production``) is refused here: it would use production credentials on a developer machine.
"""
import os
import sys
from pathlib import Path

from dotenv import load_dotenv
from waitress import serve

DEV_ENV_FILE = Path(__file__).with_name(".env.dev")

# Must run before flask_app is imported: configuration is read while the application is created.
if DEV_ENV_FILE.is_file():
    load_dotenv(DEV_ENV_FILE, encoding="utf-8-sig")
else:
    print("run_local.py: %s not found - starting with the shell environment only." % DEV_ENV_FILE.name, file=sys.stderr)
if os.environ.get("FLASK_ENV") == "production":
    sys.exit("run_local.py: FLASK_ENV=production - refusing to run a production configuration locally. Fix .env.dev.")
os.environ["PYTHON_DOTENV_DISABLED"] = "1"

from flask_app import app  # noqa: E402

if __name__ == "__main__":
    serve(app, host="0.0.0.0", port=5000)
