-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlaunch.sh
More file actions
executable file
·92 lines (82 loc) · 2.77 KB
/
Copy pathlaunch.sh
File metadata and controls
executable file
·92 lines (82 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# launch.sh - Script to launch the web server in a bare-metal environment (non-Docker)
# Ensure the script runs relative to its own directory so .env/.venv are found reliably.
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Load .env (if present) and export values for this process.
if [ -f ".env" ]; then
set -a
# shellcheck disable=SC1091
source ".env"
set +a
fi
# Default values
VENV_PATH="${VENV_PATH:-.venv}"
PORT="${PORT:-5000}"
BIND_ADDRESS="${BIND_ADDRESS:-0.0.0.0}"
ROOT_PATH="${ROOT_PATH:-}"
ACCESS_LOGFILE="${ACCESS_LOGFILE:-/dev/null}"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
PORT="$2"
shift 2
;;
-v|--venv)
VENV_PATH="$2"
shift 2
;;
-a|--access-logfile)
ACCESS_LOGFILE="$2"
shift 2
;;
-b|--bind)
BIND_ADDRESS="$2"
shift 2
;;
-r|--root-path)
ROOT_PATH="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -p, --port PORT Set the port (default: 5000)"
echo " -v, --venv PATH Set the virtual environment path (default: .venv)"
echo " -a, --access-logfile Hypercorn access log target (default: /dev/null, use '-' for stdout)"
echo " -b, --bind ADDRESS Bind address (default: 0.0.0.0, use 127.0.0.1 behind a reverse proxy)"
echo " -r, --root-path PATH Serve the app under a URL prefix, e.g. /mousesearch (default: none)"
echo " -h, --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use -h or --help for usage information"
exit 1
;;
esac
done
# Activate virtual environment
if [ ! -d "$VENV_PATH" ]; then
echo "Error: Virtual environment not found at $VENV_PATH"
exit 1
fi
source "$VENV_PATH/bin/activate"
# Launch hypercorn with specified port
EXTRA_ARGS=()
if [ -n "$ROOT_PATH" ]; then
EXTRA_ARGS+=(--root-path "$ROOT_PATH")
fi
hypercorn --bind "$BIND_ADDRESS:$PORT" --workers 1 --worker-class asyncio --access-logfile "$ACCESS_LOGFILE" --error-logfile - --log-level info "${EXTRA_ARGS[@]}" app:app 2>&1 | while IFS= read -r line; do
echo "$line"
if [[ "$line" == *"Address already in use"* ]] || [[ "$line" == *"Errno 98"* ]]; then
echo ""
echo "ERROR: Port $PORT is already in use."
echo "You can change the port using: $0 --port <PORT_NUMBER>"
echo "Example: $0 --port 8080"
exit 1
fi
done
# Capture the exit code from hypercorn
exit ${PIPESTATUS[0]}