Files

59 lines
1.7 KiB
Bash
Raw Permalink Normal View History

2026-11-01 12:07:04 +01:00
#!/usr/bin/env bash
# Development launcher. Pick the service and port:
#
# ./run.sh # main app on 8000
# ./run.sh main 8000 # main app on 8000
# ./run.sh sensors 8001 # sensors app on 8001 (localhost only)
#
# The main app calls the sensors API server-to-server. Override its address
# when the sensors app is not on localhost:8001:
#
# SENSORS_API_URL=http://192.168.0.50:9001 ./run.sh main 8000
#
# The sensors API is open to every user on the local machine. To expose it
# on the network explicitly set HOST=0.0.0.0.
set -euo pipefail
cd "$(dirname "$0")"
APP="${1:-${APP:-main}}"
PORT="${2:-${PORT:-}}"
case "$APP" in
main)
MODULE="app:create_app"
DEFAULT_PORT=8000
DEFAULT_HOST="0.0.0.0"
;;
sensors)
MODULE="sensors_app:create_app"
DEFAULT_PORT=8001
DEFAULT_HOST="127.0.0.1"
;;
*)
echo "Usage: $0 [main|sensors] [port]" >&2
exit 2
;;
esac
PORT="${PORT:-$DEFAULT_PORT}"
HOST="${HOST:-$DEFAULT_HOST}"
2026-11-01 20:04:33 +01:00
# Prevent the main app from proxying to itself. Without an explicit
# SENSORS_API_URL the client targets localhost:8001, so main must not listen
# there as well.
if [ "$APP" = "main" ] && [ -z "${SENSORS_API_URL:-}" ] && [ "$PORT" = "8001" ]; then
echo "Refusing to start main on port 8001: SENSORS_API_URL would default to" >&2
echo "http://localhost:8001 and the app would call itself recursively." >&2
echo "Use another port (for example 8000) or set SENSORS_API_URL explicitly." >&2
exit 2
fi
2026-11-01 12:07:04 +01:00
if [ -x ".venv/bin/python" ]; then
PYTHON=".venv/bin/python"
else
PYTHON="python3"
fi
echo "Starting $APP app on http://$HOST:$PORT" >&2
exec "$PYTHON" -m flask --app "$MODULE" run --host "$HOST" --port "$PORT" --debug