File size: 3,350 Bytes
bb869fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95bad73
 
 
 
bb869fd
 
 
 
 
 
 
95bad73
bb869fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95bad73
bb869fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af140e4
 
bb869fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
#!/bin/bash
# Startup script for FormatReview
# This script starts the Streamlit service and exposes it via Tailscale Serve

# Exit on error
set -e

# --- Configuration for Streamlit App ---
STREAMLIT_APP_FILE="app.py"
STREAMLIT_PORT="8504" 
STREAMLIT_PID_FILE="formatreview.pid"
STREAMLIT_LOG_FILE="formatreview.log"
# --- End Configuration ---

# Check if UV is installed
if ! command -v uv &> /dev/null; then
    echo "Error: UV is not installed. Please install UV first."
    echo "You can install UV with: pip install uv"
    exit 1
fi

# Recreate virtual environment
echo "Recreating virtual environment..."
rm -rf .venv
uv venv

# Activate virtual environment
echo "Activating virtual environment..."
source .venv/bin/activate

# Install dependencies
echo "Installing dependencies..."
uv pip install -r requirements.txt --no-cache

# Kill any existing instances of the Streamlit app
echo "Stopping any existing instances of the Streamlit app..."
if [ -f "$STREAMLIT_PID_FILE" ]; then
    OLD_STREAMLIT_PID=$(cat $STREAMLIT_PID_FILE)
    if ps -p $OLD_STREAMLIT_PID > /dev/null; then
        kill $OLD_STREAMLIT_PID
        echo "Killed existing Streamlit app process with PID $OLD_STREAMLIT_PID"
        sleep 1 # Give it time to shut down
    else
        echo "No running Streamlit app process found with PID $OLD_STREAMLIT_PID"
    fi
fi
# Also try to kill any other streamlit processes for this specific app file and port
pkill -f "streamlit run $STREAMLIT_APP_FILE --server.port $STREAMLIT_PORT" || true
sleep 1

# Start the Streamlit app
echo "Starting Streamlit app on port $STREAMLIT_PORT..."
nohup .venv/bin/python3 -m streamlit run $STREAMLIT_APP_FILE --server.port $STREAMLIT_PORT --server.headless true > $STREAMLIT_LOG_FILE 2>&1 &
echo $! > $STREAMLIT_PID_FILE

# Check if the Streamlit service started successfully
sleep 3 # Give Streamlit a bit more time to start
if ! nc -z localhost $STREAMLIT_PORT; then
    echo "Error: Failed to start Streamlit app on port $STREAMLIT_PORT."
    cat $STREAMLIT_LOG_FILE # Output log file for debugging
    exit 1
else
    echo "Streamlit app started successfully on port $STREAMLIT_PORT."
fi

# Check if Tailscale is installed
if ! command -v tailscale &> /dev/null; then
    echo "Warning: Tailscale is not installed. The app will only be available locally."
    echo "Install Tailscale to expose the service over your tailnet."
else
    # Expose the service via Tailscale Serve
    echo "Exposing Streamlit app via Tailscale Serve on port $STREAMLIT_PORT..."
    echo "Setting up Funnel on port 8443..."
    tailscale funnel --https=8443 --bg localhost:$STREAMLIT_PORT
    
    # Get the Tailscale hostname
    HOSTNAME=$(tailscale status --json | jq -r '.Self.DNSName')
    if [ -n "$HOSTNAME" ]; then
        echo "App may be available at a Tailscale URL. Check 'tailscale status' for details."
        echo "If using a funnel, it might be https://$HOSTNAME/"
    else
        echo "App is exposed via Tailscale Serve, but couldn't determine the primary hostname."
        echo "Check 'tailscale status' for details."
    fi
fi

echo "FormatReview is now running!"
echo "Local URL: http://localhost:$STREAMLIT_PORT"
echo "Log file: $STREAMLIT_LOG_FILE"
echo "PID file: $STREAMLIT_PID_FILE"
echo ""
echo "If Tailscale is active, the app should be accessible via a Tailscale funnel URL."