Skip to content

Instantly share code, notes, and snippets.

@Brayden
Last active October 4, 2024 11:57
Show Gist options
  • Save Brayden/135a1f49e3c6a088cdf924d29aeeecdc to your computer and use it in GitHub Desktop.
Save Brayden/135a1f49e3c6a088cdf924d29aeeecdc to your computer and use it in GitHub Desktop.
Install script for deploying a Starbase instance to Cloudflare
#!/bin/bash
# Function to check for jq and install if necessary
check_and_install_jq() {
if ! command -v jq &> /dev/null; then
# Try to install jq based on the system package manager
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt-get update && sudo apt-get install -y jq > /dev/null
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install jq > /dev/null
else
echo "Please install jq manually: https://stedolan.github.io/jq/download/"
exit 1
fi
fi
}
echo " "
echo "=========================================="
echo "Welcome to the StarbaseDB installation script!"
echo " "
echo "This script will deploy a Cloudflare Worker and create an Outerbase Starlink session."
echo "If you don't have a paid Cloudflare account, your deployment will fail."
echo " "
echo "IMPORTANT: You _MUST_ have a paid Cloudflare account to use SQLite in Durable Objects."
echo "=========================================="
echo " "
# Step 1: Check if jq is installed
check_and_install_jq
# Step 2: Clone the repository
echo "Cloning the repository..."
git clone [email protected]:Brayden/starbasedb.git > /dev/null 2>&1
cd starbasedb || exit
# Step 3: Generate a secure AUTHORIZATION_TOKEN and update wrangler.toml
AUTHORIZATION_TOKEN=$(openssl rand -hex 16)
sed -i '' "s/AUTHORIZATION_TOKEN = \"[^\"]*\"/AUTHORIZATION_TOKEN = \"$AUTHORIZATION_TOKEN\"/" wrangler.toml
# Step 4: Prompt the user for Cloudflare account_id (force interaction)
echo " "
echo "Please enter your Cloudflare account_id (from 'wrangler whoami' or the Cloudflare dashboard):"
read -r ACCOUNT_ID </dev/tty
sed -i '' "s/^account_id = .*/account_id = \"$ACCOUNT_ID\"/" wrangler.toml || echo "account_id = \"$ACCOUNT_ID\"" >> wrangler.toml
# Step 5: Run typegen command
npm install > /dev/null 2>&1
npm run cf-typegen > /dev/null 2>&1
# Step 6: Deploy the worker
echo " "
echo "Deploying your worker..."
DEPLOY_OUTPUT=$(npm run deploy 2>&1)
# Step 7: Extract the URL from the deploy output
HOST_URL=$(echo "$DEPLOY_OUTPUT" | grep -oE 'https://[a-zA-Z0-9.-]+\.workers\.dev')
if [ -n "$HOST_URL" ]; then
echo "Worker deployed successfully at $HOST_URL."
else
echo "Error: Failed to extract the worker URL."
exit 1
fi
# Step 8: Create Outerbase Starlink session
STARLINK_RESPONSE=$(curl --silent --request POST \
--url https://app.outerbase.com/api/v1/starlink \
--header 'Content-Type: application/json' \
--data "{
\"credentials\": {
\"database\": \"db\",
\"host\": \"$HOST_URL\",
\"user\": \"user\",
\"password\": \"pass\",
\"port\": 0,
\"type\": \"starbasedb\",
\"starbasedbOptions\": {
\"databasePath\": \"$HOST_URL\",
\"databaseToken\": \"$AUTHORIZATION_TOKEN\"
}
}
}")
# Step 9: Extract the Starlink URL
STARLINK_URL=$(echo "$STARLINK_RESPONSE" | jq -r '.response.url')
if [ -n "$STARLINK_URL" ]; then
# Display Starlink URL in a clearly visible block
echo " "
echo "=========================================="
echo "Outerbase Starlink session created!"
echo "Use the following URL to view your database:"
echo
echo "$STARLINK_URL"
echo
echo "=========================================="
else
echo "Error: Failed to create Outerbase Starlink session."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment