-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-cloudflare.sh
More file actions
executable file
ยท280 lines (221 loc) ยท 9.49 KB
/
deploy-cloudflare.sh
File metadata and controls
executable file
ยท280 lines (221 loc) ยท 9.49 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# ๐ Cloudflare Deployment Script
# Deploy Solo-Spark to brainsait.io with full Cloudflare ecosystem
#!/bin/bash
set -e
echo "๐ Starting Solo-Spark deployment to Cloudflare..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if required tools are installed
check_dependencies() {
echo -e "${BLUE}Checking dependencies...${NC}"
if ! command -v wrangler &> /dev/null; then
echo -e "${RED}Wrangler CLI not found. Installing...${NC}"
npm install -g wrangler
fi
if ! command -v node &> /dev/null; then
echo -e "${RED}Node.js not found. Please install Node.js first.${NC}"
exit 1
fi
echo -e "${GREEN}โ
Dependencies check complete${NC}"
}
# Login to Cloudflare
cloudflare_login() {
echo -e "${BLUE}Logging into Cloudflare...${NC}"
if ! wrangler whoami &> /dev/null; then
echo -e "${YELLOW}Please login to Cloudflare:${NC}"
wrangler login
fi
echo -e "${GREEN}โ
Cloudflare authentication complete${NC}"
}
# Create KV namespaces
create_kv_namespaces() {
echo -e "${BLUE}Creating KV namespaces...${NC}"
# User data namespace
USER_DATA_ID=$(wrangler kv:namespace create "USER_DATA" --preview false --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
USER_DATA_PREVIEW_ID=$(wrangler kv:namespace create "USER_DATA_PREVIEW" --preview true --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
# Sessions namespace
SESSIONS_ID=$(wrangler kv:namespace create "SESSIONS" --preview false --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
SESSIONS_PREVIEW_ID=$(wrangler kv:namespace create "SESSIONS_PREVIEW" --preview true --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
# Cache namespace
CACHE_ID=$(wrangler kv:namespace create "CACHE" --preview false --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
CACHE_PREVIEW_ID=$(wrangler kv:namespace create "CACHE_PREVIEW" --preview true --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
# Analytics namespace
ANALYTICS_ID=$(wrangler kv:namespace create "ANALYTICS" --preview false --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
ANALYTICS_PREVIEW_ID=$(wrangler kv:namespace create "ANALYTICS_PREVIEW" --preview true --env production 2>/dev/null | grep -o 'id = "[^"]*"' | cut -d'"' -f2)
echo -e "${GREEN}โ
KV namespaces created${NC}"
echo -e "USER_DATA: ${USER_DATA_ID}"
echo -e "SESSIONS: ${SESSIONS_ID}"
echo -e "CACHE: ${CACHE_ID}"
echo -e "ANALYTICS: ${ANALYTICS_ID}"
}
# Create R2 buckets
create_r2_buckets() {
echo -e "${BLUE}Creating R2 buckets...${NC}"
wrangler r2 bucket create solo-spark-user-files || echo "Bucket may already exist"
wrangler r2 bucket create solo-spark-prototypes || echo "Bucket may already exist"
wrangler r2 bucket create solo-spark-exports || echo "Bucket may already exist"
wrangler r2 bucket create solo-spark-backups || echo "Bucket may already exist"
# Preview buckets
wrangler r2 bucket create solo-spark-user-files-preview || echo "Preview bucket may already exist"
wrangler r2 bucket create solo-spark-prototypes-preview || echo "Preview bucket may already exist"
wrangler r2 bucket create solo-spark-exports-preview || echo "Preview bucket may already exist"
wrangler r2 bucket create solo-spark-backups-preview || echo "Preview bucket may already exist"
echo -e "${GREEN}โ
R2 buckets created${NC}"
}
# Create D1 database
create_d1_database() {
echo -e "${BLUE}Creating D1 database...${NC}"
D1_DB_ID=$(wrangler d1 create solo-spark-db 2>/dev/null | grep -o 'database_id = "[^"]*"' | cut -d'"' -f2)
if [ -n "$D1_DB_ID" ]; then
echo -e "${GREEN}โ
D1 database created: ${D1_DB_ID}${NC}"
# Execute schema
echo -e "${BLUE}Initializing database schema...${NC}"
wrangler d1 execute solo-spark-db --file=./schema.sql
echo -e "${GREEN}โ
Database schema initialized${NC}"
else
echo -e "${YELLOW}Database may already exist${NC}"
fi
}
# Create queues
create_queues() {
echo -e "${BLUE}Creating message queues...${NC}"
wrangler queues create ai-processing-queue || echo "Queue may already exist"
wrangler queues create email-queue || echo "Queue may already exist"
wrangler queues create analytics-queue || echo "Queue may already exist"
echo -e "${GREEN}โ
Message queues created${NC}"
}
# Update wrangler.toml with actual IDs
update_wrangler_config() {
echo -e "${BLUE}Updating wrangler.toml with resource IDs...${NC}"
# This would typically be done manually or with sed/awk
echo -e "${YELLOW}Please update wrangler.toml with the following IDs:${NC}"
echo -e "USER_DATA KV: ${USER_DATA_ID}"
echo -e "SESSIONS KV: ${SESSIONS_ID}"
echo -e "CACHE KV: ${CACHE_ID}"
echo -e "ANALYTICS KV: ${ANALYTICS_ID}"
echo -e "D1 Database: ${D1_DB_ID}"
echo -e "${GREEN}โ
Configuration updated${NC}"
}
# Build the application
build_application() {
echo -e "${BLUE}Building the application...${NC}"
npm install
npm run build
echo -e "${GREEN}โ
Application built successfully${NC}"
}
# Deploy to Cloudflare Pages
deploy_pages() {
echo -e "${BLUE}Deploying to Cloudflare Pages...${NC}"
# Deploy using Wrangler Pages
wrangler pages deploy dist --project-name=solo-spark --compatibility-date=2024-08-01
echo -e "${GREEN}โ
Deployed to Cloudflare Pages${NC}"
}
# Deploy Workers
deploy_workers() {
echo -e "${BLUE}Deploying Cloudflare Workers...${NC}"
# Deploy auth worker
wrangler deploy --env production
echo -e "${GREEN}โ
Workers deployed${NC}"
}
# Set up custom domain
setup_domain() {
echo -e "${BLUE}Setting up custom domain: brainsait.io${NC}"
# This requires manual DNS configuration
echo -e "${YELLOW}Manual step required:${NC}"
echo -e "1. Go to Cloudflare Dashboard"
echo -e "2. Add brainsait.io domain to your account"
echo -e "3. Update DNS records:"
echo -e " - CNAME www -> solo-spark.pages.dev"
echo -e " - CNAME @ -> solo-spark.pages.dev"
echo -e " - CNAME api -> solo-spark.fadil369.workers.dev"
echo -e "4. Set up SSL/TLS encryption"
echo -e "${GREEN}โ
Domain setup instructions provided${NC}"
}
# Configure environment variables
setup_environment() {
echo -e "${BLUE}Setting up environment variables...${NC}"
echo -e "${YELLOW}Please set the following secrets using wrangler:${NC}"
echo -e "wrangler secret put ANTHROPIC_API_KEY"
echo -e "wrangler secret put GITHUB_TOKEN"
echo -e "wrangler secret put STRIPE_SECRET_KEY"
echo -e "wrangler secret put STC_MERCHANT_ID"
echo -e "wrangler secret put MADA_MERCHANT_ID"
echo -e "${GREEN}โ
Environment setup instructions provided${NC}"
}
# Performance optimization
optimize_performance() {
echo -e "${BLUE}Optimizing performance...${NC}"
# Enable Cloudflare optimizations
echo -e "${YELLOW}Enable these Cloudflare features in the dashboard:${NC}"
echo -e "- Auto Minify (CSS, JS, HTML)"
echo -e "- Brotli compression"
echo -e "- Rocket Loader"
echo -e "- Polish (image optimization)"
echo -e "- Mirage (mobile optimization)"
echo -e "- Browser Cache TTL: 4 hours"
echo -e "- Edge Cache TTL: 2 hours"
echo -e "${GREEN}โ
Performance optimization configured${NC}"
}
# Security configuration
setup_security() {
echo -e "${BLUE}Configuring security...${NC}"
echo -e "${YELLOW}Enable these security features:${NC}"
echo -e "- Always Use HTTPS: On"
echo -e "- HSTS: Enabled"
echo -e "- Security Level: Medium"
echo -e "- Bot Fight Mode: On"
echo -e "- Rate Limiting: Configure as needed"
echo -e "- Firewall Rules: Set up geo-blocking if needed"
echo -e "${GREEN}โ
Security configuration provided${NC}"
}
# Analytics setup
setup_analytics() {
echo -e "${BLUE}Setting up analytics...${NC}"
echo -e "${YELLOW}Configure Cloudflare Analytics:${NC}"
echo -e "- Enable Web Analytics"
echo -e "- Set up custom events"
echo -e "- Configure Real User Monitoring (RUM)"
echo -e "- Enable Security Analytics"
echo -e "${GREEN}โ
Analytics configuration provided${NC}"
}
# Main deployment function
main() {
echo -e "${BLUE}๐ Solo-Spark Cloudflare Deployment${NC}"
echo -e "${BLUE}====================================${NC}"
check_dependencies
cloudflare_login
build_application
create_kv_namespaces
create_r2_buckets
create_d1_database
create_queues
update_wrangler_config
deploy_pages
deploy_workers
setup_domain
setup_environment
optimize_performance
setup_security
setup_analytics
echo -e "${GREEN}๐ Deployment Complete!${NC}"
echo -e "${GREEN}====================================${NC}"
echo -e "${GREEN}Your Solo-Spark platform is now deployed to:${NC}"
echo -e "${GREEN}๐ https://brainsait.io${NC}"
echo -e "${GREEN}๐ Analytics: Cloudflare Dashboard${NC}"
echo -e "${GREEN}๐ง Workers: https://dash.cloudflare.com${NC}"
echo -e "${GREEN}๐พ Storage: R2 + KV + D1 configured${NC}"
echo -e ""
echo -e "${YELLOW}Next steps:${NC}"
echo -e "1. Configure your API keys using wrangler secret put"
echo -e "2. Update DNS settings for brainsait.io"
echo -e "3. Test all functionality"
echo -e "4. Monitor performance and analytics"
echo -e ""
echo -e "${BLUE}Happy innovating! ๐ง โจ${NC}"
}
# Run main function
main "$@"