Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·165 lines (133 loc) · 6.15 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·165 lines (133 loc) · 6.15 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
set -euo pipefail
# cloudflare-uptime — First-time setup
# Usage: ./setup.sh
#
# This script walks through every step needed to deploy the Worker for the first time.
# It will NOT modify wrangler.toml automatically — you will be prompted to paste values.
BOLD='\033[1m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
RESET='\033[0m'
echo ""
echo -e "${BOLD}=== cloudflare-uptime Setup ===${RESET}"
echo ""
# ── Prerequisites check ──────────────────────────────────────────────────────
echo -e "${CYAN}Checking prerequisites...${RESET}"
if ! command -v node >/dev/null 2>&1; then
echo "Error: Node.js is required. Install it from https://nodejs.org/ (v20 or later)."
exit 1
fi
NODE_VERSION=$(node --version | sed 's/v//' | cut -d. -f1)
if [ "$NODE_VERSION" -lt 20 ]; then
echo "Error: Node.js 20 or later is required (found $(node --version))."
exit 1
fi
if ! command -v npm >/dev/null 2>&1; then
echo "Error: npm is required. It is bundled with Node.js."
exit 1
fi
echo -e "${GREEN}Node.js $(node --version) found.${RESET}"
# ── Install dependencies ─────────────────────────────────────────────────────
echo ""
echo -e "${CYAN}Installing dependencies...${RESET}"
npm install
echo -e "${GREEN}Dependencies installed.${RESET}"
# Wrangler is now available via npx after npm install
WRANGLER="npx wrangler"
# ── Cloudflare auth ───────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 1: Cloudflare authentication${RESET}"
echo ""
echo "If you are not already logged in, Wrangler will open a browser window."
echo "Press Enter to continue, or Ctrl-C to abort and log in manually with: npx wrangler login"
read -r
$WRANGLER whoami 2>/dev/null || {
echo ""
echo "Running wrangler login..."
$WRANGLER login
}
# ── D1 database ───────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 2: Create D1 database${RESET}"
echo ""
echo "This will create a D1 database named 'uptime-monitor' in your Cloudflare account."
echo "Press Enter to continue, or Ctrl-C to skip (if the database already exists)."
read -r
echo ""
$WRANGLER d1 create uptime-monitor || true
echo ""
echo -e "${YELLOW}ACTION REQUIRED:${RESET}"
echo "Copy the 'database_id' from the output above."
echo "Open wrangler.toml and paste it into the database_id field:"
echo ""
echo " [[d1_databases]]"
echo " binding = \"DB\""
echo " database_name = \"uptime-monitor\""
echo " database_id = \"PASTE_HERE\""
echo ""
echo "Press Enter once you have updated wrangler.toml..."
read -r
# ── Schema ────────────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 3: Apply database schema${RESET}"
echo ""
echo "Applying schema.sql to the remote D1 database..."
$WRANGLER d1 execute uptime-monitor --remote --file=schema.sql
echo -e "${GREEN}Schema applied.${RESET}"
# ── R2 bucket ────────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 4: Create R2 bucket${RESET}"
echo ""
echo " (Optional: required only for uploading logos)"
echo ""
echo "This will create an R2 bucket named 'uptime-assets' for logo uploads."
echo "Press Enter to create it, or type 's' and press Enter to skip."
read -r R2_SKIP
if [[ "$R2_SKIP" != "s" && "$R2_SKIP" != "S" ]]; then
$WRANGLER r2 bucket create uptime-assets || true
echo ""
echo "If you used a different bucket name, update wrangler.toml:"
echo ""
echo " [[r2_buckets]]"
echo " binding = \"ASSETS\""
echo " bucket_name = \"your-bucket-name\""
echo ""
else
echo -e "${YELLOW}Skipping R2 bucket creation.${RESET}"
echo "Note: You can remove or comment out the [[r2_buckets]] section in wrangler.toml."
fi
# ── API key secret ────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 5: Set API key secret${RESET}"
echo ""
echo "You will be prompted to enter your API key. Generate a strong one with:"
echo " openssl rand -hex 32"
echo ""
echo "This key authenticates all admin API requests (X-API-Key header)."
echo "Press Enter to run: wrangler secret put API_KEY"
read -r
$WRANGLER secret put API_KEY
# ── Deploy ────────────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Step 6: Deploy${RESET}"
echo ""
echo "Ready to deploy the Worker. Press Enter to run: npm run deploy"
echo "(Ctrl-C to skip and deploy manually later)"
read -r
npm run deploy
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}${BOLD}=== Setup complete! ===${RESET}"
echo ""
echo "Your Worker is now live. Next steps:"
echo ""
echo " 1. Open the admin dashboard at your Worker URL (shown above) and add monitors"
echo " 2. Create a status page and assign monitors to it"
echo " 3. Share /status/<slug> with your users"
echo " 4. (Optional) Add custom domain routes in wrangler.toml under [[routes]]"
echo " 5. (Optional) Add CLOUDFLARE_API_TOKEN as a GitHub Actions secret for auto-deploy"
echo ""
echo -e "${CYAN}Using Claude Code? Run 'claude' in this directory -- CLAUDE.md has full context.${RESET}"
echo ""