#!/usr/bin/env bash
#
# Restored Perspective Counselling Ltd. — full deploy script
# Cloudflare Pages (frontend) + Fly.io Toronto (booking API backend)
#
# Run this from your own machine, from inside the “restored-perspective”
# project folder (the one containing frontend/ and server/).
#
# BEFORE RUNNING:
# 1. Register the domain first (Cloudflare Registrar) — this step needs
# the Cloudflare dashboard/checkout, so it isn’t scripted. See
# DEPLOYMENT.md, Step 1.
# 2. Create a free Cloudflare account (for Pages) and a Fly.io account
# (for the API) if you don’t have them yet — Fly.io needs a credit
# card on file even though usage is billed monthly, pay-as-you-go.
# 3. Have your Stripe test key ready (dashboard.stripe.com -> Developers
# -> API keys -> the sk_test_… key). You’ll be prompted for it below;
# it’s read silently (won’t echo to the terminal) and never written to
# this script.
#
# This script pauses before anything that creates a billed resource so you
# can back out safely — read each prompt before hitting enter.
set -euo pipefail
PROJECT_ROOT=”$(cd “$(dirname “${BASH_SOURCE[0]}”)” && pwd)”
FRONTEND_DIR=”$PROJECT_ROOT/frontend”
SERVER_DIR=”$PROJECT_ROOT/server”
confirm() {
read -r -p “$1 [y/N] ” reply
[[ “$reply” =~ ^[Yy]$ ]]
}
echo “==========================================================”
echo ” Restored Perspective Counselling Ltd. — deploy script”
echo “==========================================================”
echo
if [[ ! -d “$FRONTEND_DIR” || ! -d “$SERVER_DIR” ]]; then
echo “Error: run this script from inside the restored-perspective project”
echo “folder (couldn’t find frontend/ and server/ next to deploy.sh).”
exit 1
fi
# —- Prerequisite checks ———————————————–
echo “– Checking prerequisites –”
if ! command -v node >/dev/null 2>&1; then
echo “Node.js is required (for npx/wrangler) but wasn’t found.”
echo “Install it from https://nodejs.org/ (LTS version) and re-run this script.”
exit 1
fi
echo ” node: $(node –version)”
if ! command -v flyctl >/dev/null 2>&1; then
echo ” flyctl not found — installing it now…”
curl -L https://fly.io/install.sh | sh
export FLYCTL_INSTALL=”$HOME/.fly”
export PATH=”$FLYCTL_INSTALL/bin:$PATH”
if ! command -v flyctl >/dev/null 2>&1; then
echo “flyctl installed but not on PATH yet. Open a new terminal (so your”
echo “shell profile picks up the PATH change) and re-run this script.”
exit 1
fi
fi
echo ” flyctl: $(flyctl version)”
echo
# =========================================================================
# PART 1 — Frontend: deploy the static site to Cloudflare Pages
# =========================================================================
echo “– Part 1: Frontend (Cloudflare Pages) –”
echo
if confirm “Deploy the frontend to Cloudflare Pages now?”; then
echo “Logging into Cloudflare (a browser tab will open)…”
npx wrangler login
read -r -p “Cloudflare Pages project name [restored-perspective]: ” PAGES_PROJECT
PAGES_PROJECT=”${PAGES_PROJECT:-restored-perspective}”
# Create the project if it doesn’t already exist. If it does, this step
# will just report that and we move on to deploying.
npx wrangler pages project create “$PAGES_PROJECT” –production-branch=main || true
echo “Deploying $FRONTEND_DIR to Cloudflare Pages project ‘$PAGES_PROJECT’…”
npx wrangler pages deploy “$FRONTEND_DIR” –project-name=”$PAGES_PROJECT”
echo
echo “Frontend deployed. It’s live at https://$PAGES_PROJECT.pages.dev”
echo “To attach your real domain: Cloudflare dashboard -> Workers & Pages ->”
echo “$PAGES_PROJECT -> Custom domains -> Add your domain (DEPLOYMENT.md, Step 2.4).”
else
echo “Skipping frontend deploy.”
fi
echo
# =========================================================================
# PART 2 — Backend: deploy the booking API to Fly.io (Toronto)
# =========================================================================
echo “– Part 2: Backend API (Fly.io, Toronto/yyz) –”
echo
if confirm “Set up and deploy the booking API to Fly.io now?”; then
cd “$SERVER_DIR”
echo “Logging into Fly.io (a browser tab will open)…”
flyctl auth login
echo
echo “Launching the app from server/fly.toml (app name/region are already”
echo “set there — you’ll be asked to confirm or rename if the app name is taken)…”
flyctl launch –no-deploy –copy-config –yes
echo
if confirm “Create the persistent data volume ‘rp_data’ in yyz (Toronto) now? (one-time, needed before first deploy)”; then
flyctl volumes create rp_data –region yyz –size 1 –yes
else
echo “Skipped — you must create this before ‘flyctl deploy’ will work,”
echo “since fly.toml mounts it. Run manually later:”
echo ” flyctl volumes create rp_data –region yyz –size 1″
fi
echo
echo “– Stripe secrets –”
echo “Enter your Stripe SECRET key (starts with sk_test_ or sk_live_).”
echo “Input is hidden and is sent straight to ‘flyctl secrets set’ — it is”
echo “never saved to disk or shown on screen.”
read -r -s -p “STRIPE_SECRET_KEY: ” STRIPE_SECRET_KEY
echo
if [[ -n “$STRIPE_SECRET_KEY” ]]; then
flyctl secrets set “STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY”
else
echo ” (left blank — skipping. The API will run in mock-payment mode until set.)”
fi
read -r -s -p “STRIPE_WEBHOOK_SECRET (leave blank to add later, whsec_…): ” STRIPE_WEBHOOK_SECRET
echo
if [[ -n “$STRIPE_WEBHOOK_SECRET” ]]; then
flyctl secrets set “STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET”
fi
unset STRIPE_SECRET_KEY STRIPE_WEBHOOK_SECRET
echo
read -r -p “Your live frontend URL for FRONTEND_BASE_URL [https://restoredperspectivecounselling.ca]: ” FRONTEND_URL
FRONTEND_URL=”${FRONTEND_URL:-https://restoredperspectivecounselling.ca}”
flyctl secrets set “FRONTEND_BASE_URL=$FRONTEND_URL” –stage
# (fly.toml already sets FRONTEND_BASE_URL as a plain env var; this
# –stage’d secret is only applied if you deploy again below, and simply
# overrides it if you typed a different URL than what’s in fly.toml.)
echo
if confirm “Deploy the API now (flyctl deploy)?”; then
flyctl deploy
else
echo “Skipped. Run ‘flyctl deploy’ from server/ whenever you’re ready.”
fi
echo
read -r -p “Custom API domain to request an SSL cert for (blank to skip) [api.restoredperspectivecounselling.ca]: ” API_DOMAIN
if [[ -n “${API_DOMAIN:-}” ]]; then
flyctl certs add “$API_DOMAIN”
echo
echo “Now add the DNS record flyctl just printed above to your domain’s”
echo “DNS (Cloudflare dashboard -> DNS), then re-run:”
echo ” flyctl certs check $API_DOMAIN”
echo “until it shows the certificate is issued.”
fi
cd “$PROJECT_ROOT”
else
echo “Skipping backend deploy.”
fi
echo
echo “==========================================================”
echo ” Done. Remaining manual steps:”
echo ” 1. Point window.RP_API_BASE in frontend/booking.html and”
echo ” frontend/mock-payment.html at your real API URL, then re-run”
echo ” Part 1 above to redeploy the frontend.”
echo ” 2. Test a full booking with Stripe test card 4242 4242 4242 4242″
echo ” before switching to a live Stripe key.”
echo ” 3. See DEPLOYMENT.md for anything not covered here.”
echo “==========================================================”