diff --git a/.env.example b/.env.example index 46f1bfb..7f6685d 100644 --- a/.env.example +++ b/.env.example @@ -20,6 +20,8 @@ ASSIGNMENT_TIMEOUT=60 ASSIGNMENT_REMINDER=45 CHAIN_TIMER_THRESHOLD=5 -# Server Configuration (optional) -# HOST=0.0.0.0 -# PORT=8000 +# Server Configuration +# HOST: Set to 0.0.0.0 to allow external connections, or 127.0.0.1 for localhost only +HOST=127.0.0.1 +# PORT: Port to run the application on +PORT=8000 diff --git a/main.py b/main.py index 2d41aa6..bc06567 100644 --- a/main.py +++ b/main.py @@ -100,9 +100,15 @@ async def start_bot(): async def main(): # Parse command-line arguments import argparse + import os + + # Read from environment variables first, then fall back to defaults + default_port = int(os.getenv("PORT", "8000")) + default_host = os.getenv("HOST", "127.0.0.1") + parser = argparse.ArgumentParser(description="Faction War Dispatch Bot") - parser.add_argument("--port", type=int, default=8000, help="Port to run the application on (default: 8000)") - parser.add_argument("--host", type=str, default="127.0.0.1", help="Host to bind to (default: 127.0.0.1)") + parser.add_argument("--port", type=int, default=default_port, help=f"Port to run the application on (default: {default_port})") + parser.add_argument("--host", type=str, default=default_host, help=f"Host to bind to (default: {default_host})") args = parser.parse_args() # Start Discord bot in background diff --git a/setup_production.sh b/setup_production.sh index fe822b2..4eed18c 100644 --- a/setup_production.sh +++ b/setup_production.sh @@ -87,6 +87,10 @@ echo "" echo "Setting up environment file..." if [ ! -f ".env" ]; then cp .env.example .env + + # Update PORT in .env file + sed -i "s/^PORT=.*/PORT=$APP_PORT/" .env + echo "" echo "IMPORTANT: You need to configure /opt/faction-war/app/.env with your secrets!" echo "" @@ -94,6 +98,8 @@ if [ ! -f ".env" ]; then echo " - AUTH_PASSWORD: openssl rand -base64 32" echo " - JWT_SECRET: openssl rand -hex 64" echo "" + echo "Application port has been set to: $APP_PORT" + echo "" echo "You can either:" echo " 1. Edit the .env file now" echo " 2. Skip and edit it later before starting the application" @@ -112,8 +118,8 @@ chmod 600 .env echo "" echo "Setting up systemd service..." -# Update the service file with the configured port -sed "s|ExecStart=.*|ExecStart=/opt/faction-war/app/venv/bin/python main.py --port $APP_PORT|g" faction-war.service > /etc/systemd/system/faction-war.service +# Copy the service file +cp faction-war.service /etc/systemd/system/faction-war.service systemctl daemon-reload systemctl enable faction-war systemctl start faction-war @@ -159,6 +165,7 @@ echo "" echo "Important next steps:" echo "1. Configure /opt/faction-war/app/.env with your secrets (if not done already)" echo " - Edit with: sudo -u factionwar nano /opt/faction-war/app/.env" +echo " - Change PORT or HOST in .env if needed" echo " - Then restart: sudo systemctl restart faction-war" echo "2. Access the application:" echo " - Local: http://localhost:$APP_PORT"