rybbit/setup.sh
Bill Yang 7efb818c3d Refactor environment variable configuration and enhance setup script
- Updated `.env.example` to use HOST_BACKEND_PORT and HOST_CLIENT_PORT for clearer port mapping.
- Modified `docker-compose.yml` to reflect changes in environment variable names for backend and client services.
- Enhanced `setup.sh` to support custom port options and improved error handling for missing `.env` file.
- Updated documentation in `self-hosting.mdx` to include new setup script options and examples for custom port configurations.
2025-05-06 11:27:21 -07:00

139 lines
No EOL
3.9 KiB
Bash

#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Default values
USE_WEBSERVER="true"
BACKEND_PORT="3001"
CLIENT_PORT="3002"
HOST_BACKEND_PORT="127.0.0.1:3001"
HOST_CLIENT_PORT="127.0.0.1:3002"
# Help function
show_help() {
echo "Usage: $0 <domain_name> [options]"
echo "Example: $0 myapp.example.com"
echo "Example with no webserver: $0 myapp.example.com --no-webserver"
echo ""
echo "Options:"
echo " --no-webserver Disable the built-in Caddy webserver"
echo " --backend-port <port> Set custom host port for backend (default: 3001)"
echo " --client-port <port> Set custom host port for client (default: 3002)"
echo " --help Show this help message"
}
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--no-webserver)
USE_WEBSERVER="false"
shift
;;
--backend-port)
if [[ -z "$2" || "$2" =~ ^- ]]; then
echo "Error: --backend-port requires a port number"
show_help
exit 1
fi
BACKEND_PORT="$2"
shift 2
;;
--client-port)
if [[ -z "$2" || "$2" =~ ^- ]]; then
echo "Error: --client-port requires a port number"
show_help
exit 1
fi
CLIENT_PORT="$2"
shift 2
;;
--help)
show_help
exit 0
;;
-*)
echo "Unknown option: $1"
show_help
exit 1
;;
*)
if [ -z "$DOMAIN_NAME" ]; then
DOMAIN_NAME="$1"
else
echo "Error: Only one domain name can be specified"
show_help
exit 1
fi
shift
;;
esac
done
# Update port mappings after all args are parsed
if [ "$USE_WEBSERVER" = "false" ]; then
# When not using the built-in webserver, expose ports to host
HOST_BACKEND_PORT="${BACKEND_PORT}:3001"
HOST_CLIENT_PORT="${CLIENT_PORT}:3002"
else
# Keep ports only accessible via localhost when using Caddy
HOST_BACKEND_PORT="127.0.0.1:3001"
HOST_CLIENT_PORT="127.0.0.1:3002"
fi
# Check if domain name argument is provided
if [ -z "$DOMAIN_NAME" ]; then
echo "Error: Domain name is required"
show_help
exit 1
fi
BASE_URL="https://${DOMAIN_NAME}"
# Generate a secure random secret for BETTER_AUTH_SECRET
# Uses OpenSSL if available, otherwise falls back to /dev/urandom
if command -v openssl &> /dev/null; then
BETTER_AUTH_SECRET=$(openssl rand -hex 32)
elif [ -e /dev/urandom ]; then
BETTER_AUTH_SECRET=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)
else
echo "Error: Could not generate secure secret. Please install openssl or ensure /dev/urandom is available." >&2
exit 1
fi
# Create or overwrite the .env file
echo "Creating .env file..."
cat > .env << EOL
# Variables configured by setup.sh
DOMAIN_NAME=${DOMAIN_NAME}
BASE_URL=${BASE_URL}
BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET}
DISABLE_SIGNUP=false
USE_WEBSERVER=${USE_WEBSERVER}
HOST_BACKEND_PORT=${HOST_BACKEND_PORT}
HOST_CLIENT_PORT=${HOST_CLIENT_PORT}
EOL
echo ".env file created successfully with domain ${DOMAIN_NAME}."
if [ "$USE_WEBSERVER" = "false" ]; then
echo "Caddy webserver is disabled. You'll need to set up your own webserver."
if [ "$BACKEND_PORT" = "3001" ] && [ "$CLIENT_PORT" = "3002" ]; then
echo "The backend service will be available on port 3001 and the client on port 3002."
else
echo "The backend service will be available on port ${BACKEND_PORT} (mapped to container port 3001)"
echo "The client service will be available on port ${CLIENT_PORT} (mapped to container port 3002)"
fi
fi
# Build and start the Docker Compose stack
echo "Building and starting Docker services..."
if [ "$USE_WEBSERVER" = "false" ]; then
# Start without the caddy service when using --no-webserver
docker compose up --build -d backend client clickhouse postgres
else
# Start all services including caddy
docker compose up --build -d
fi
echo "Setup complete. Services are starting in the background."
echo "You can monitor logs with: docker compose logs -f"