Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·850 lines (728 loc) · 28.7 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·850 lines (728 loc) · 28.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#!/usr/bin/env bash
# ==============================================================================
# Base Project — Interactive Setup Wizard
# A beautiful TUI-style configurator for your Django backend project
# Works on: Linux, macOS, Windows (Git Bash / WSL / MSYS2)
# ==============================================================================
set -Eeuo pipefail
IFS=$'\n\t'
# ─────────────────────────────────────────────
# OS Detection
# ─────────────────────────────────────────────
detect_os() {
case "$(uname -s)" in
Linux*) OS_TYPE="linux" ;;
Darwin*) OS_TYPE="macos" ;;
CYGWIN*|MINGW*|MSYS*) OS_TYPE="windows" ;;
*) OS_TYPE="unknown" ;;
esac
}
# ─────────────────────────────────────────────
# Colors & Styles
# ─────────────────────────────────────────────
setup_colors() {
if [ -t 1 ] && command -v tput >/dev/null 2>&1 && [ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]; then
BOLD='\033[1m'
DIM='\033[2m'
RESET='\033[0m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTA='\033[35m'
CYAN='\033[36m'
WHITE='\033[37m'
BR_RED='\033[91m'
BR_GREEN='\033[92m'
BR_YELLOW='\033[93m'
BR_BLUE='\033[94m'
BR_MAGENTA='\033[95m'
BR_CYAN='\033[96m'
BR_WHITE='\033[97m'
BG_BLUE='\033[44m'
BG_CYAN='\033[46m'
else
BOLD='' DIM='' RESET=''
RED='' GREEN='' YELLOW='' BLUE='' MAGENTA='' CYAN='' WHITE=''
BR_RED='' BR_GREEN='' BR_YELLOW='' BR_BLUE='' BR_MAGENTA='' BR_CYAN='' BR_WHITE=''
BG_BLUE='' BG_CYAN=''
fi
}
# ─────────────────────────────────────────────
# Global Variables
# ─────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_NAME=""
PROJECT_SLUG=""
RUN_MODE=""
DB_CHOICE=""
USE_MINIO="false"
USE_REDIS="false"
USE_CELERY="false"
ADMIN_USERNAME="admin"
ADMIN_EMAIL="admin@example.com"
ADMIN_PASSWORD="adminpass123"
DB_NAME=""
DB_USER=""
DB_PASSWORD=""
DB_HOST=""
DB_PORT="5432"
SECRET_KEY=""
TOTAL_STEPS=6
PREVIOUS_PROJECT_SLUG=""
PREVIOUS_PROJECT_DASH=""
# ─────────────────────────────────────────────
# Terminal Helpers
# ─────────────────────────────────────────────
get_term_width() {
local w
w=$(tput cols 2>/dev/null || echo 60)
if [ "$w" -gt 80 ]; then w=80; fi
if [ "$w" -lt 40 ]; then w=40; fi
echo "$w"
}
clear_screen() {
printf '\033[2J\033[H'
}
repeat_char() {
local char="$1" count="$2"
local i
for ((i=0; i<count; i++)); do printf '%s' "$char"; done
}
# ─────────────────────────────────────────────
# Box Drawing
# ─────────────────────────────────────────────
BOX_W=56
box_top() { printf " ${CYAN}╭"; repeat_char '─' $BOX_W; printf "╮${RESET}\n"; }
box_bottom() { printf " ${CYAN}╰"; repeat_char '─' $BOX_W; printf "╯${RESET}\n"; }
box_sep() { printf " ${CYAN}├"; repeat_char '─' $BOX_W; printf "┤${RESET}\n"; }
box_empty() { printf " ${CYAN}│${RESET}%-${BOX_W}s${CYAN}│${RESET}\n" ""; }
box_line() {
local text="$1"
local pad=$((BOX_W - ${#text}))
[ "$pad" -lt 0 ] && pad=0
printf " ${CYAN}│${RESET} %s%*s${CYAN}│${RESET}\n" "$text" "$((pad - 1))" ""
}
box_center() {
local text="$1"
local len=${#text}
local left=$(( (BOX_W - len) / 2 ))
local right=$(( BOX_W - len - left ))
printf " ${CYAN}│${RESET}%*s%s%*s${CYAN}│${RESET}\n" "$left" "" "$text" "$right" ""
}
box_header() {
local text="$1"
local len=${#text}
local left=$(( (BOX_W - len - 2) / 2 ))
local right=$(( BOX_W - len - 2 - left ))
printf " ${CYAN}│${BG_BLUE}${BR_WHITE}${BOLD}%*s %s %*s${RESET}${CYAN}│${RESET}\n" "$left" "" "$text" "$right" ""
}
# ─────────────────────────────────────────────
# Progress Bar
# ─────────────────────────────────────────────
show_progress() {
local current="$1"
local total="$2"
local bar_width=$((BOX_W - 12))
local filled=$(( (current * bar_width) / total ))
local empty=$((bar_width - filled))
printf " ${CYAN}│${RESET} "
printf "${BR_CYAN}"
repeat_char '█' "$filled"
printf "${DIM}"
repeat_char '░' "$empty"
printf "${RESET}"
printf " ${BOLD}%d/%d" "$current" "$total"
local right=$((BOX_W - bar_width - 8))
printf "%*s${CYAN}│${RESET}\n" "$right" ""
}
# ─────────────────────────────────────────────
# Step Header
# ─────────────────────────────────────────────
step_header() {
local step="$1"
local title="$2"
clear_screen
printf "\n"
box_top
show_progress "$step" "$TOTAL_STEPS"
box_sep
box_header "$title"
box_bottom
printf "\n"
}
# ─────────────────────────────────────────────
# UI Components
# ─────────────────────────────────────────────
print_success() { printf " ${BR_GREEN}✓${RESET} ${GREEN}%s${RESET}\n" "$1"; }
print_warning() { printf " ${BR_YELLOW}⚠${RESET} ${YELLOW}%s${RESET}\n" "$1"; }
print_error() { printf " ${BR_RED}✗${RESET} ${RED}%s${RESET}\n" "$1"; }
print_info() { printf " ${BR_BLUE}ℹ${RESET} ${DIM}%s${RESET}\n" "$1"; }
print_arrow() { printf " ${BR_MAGENTA}›${RESET} ${WHITE}%s${RESET}\n" "$1"; }
assign_var() {
local var_name="$1" value="$2"
printf -v "$var_name" '%s' "$value"
}
prompt_input() {
local prompt="$1" default="$2" var_name="$3" input
if [ -n "$default" ]; then
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET} ${DIM}(%s)${RESET}: " "$prompt" "$default"
else
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET}: " "$prompt"
fi
read -r input
[ -z "$input" ] && [ -n "$default" ] && input="$default"
assign_var "$var_name" "$input"
}
prompt_password() {
local prompt="$1" default="$2" var_name="$3" input
if [ -n "$default" ]; then
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET} ${DIM}(hidden, default set)${RESET}: " "$prompt"
else
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET} ${DIM}(hidden)${RESET}: " "$prompt"
fi
read -rs input
printf "\n"
[ -z "$input" ] && [ -n "$default" ] && input="$default"
assign_var "$var_name" "$input"
}
# FIXED: All display output goes to stderr so subshell capture works
prompt_choice() {
local prompt="$1"
shift
local options=("$@")
local count=${#options[@]}
local choice=""
printf "\n" >&2
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET}\n" "$prompt" >&2
printf "\n" >&2
for i in "${!options[@]}"; do
local num=$((i + 1))
printf " ${CYAN}┌─${RESET}\n" >&2
printf " ${CYAN}│${RESET} ${BOLD}${BR_WHITE} %d ${RESET} %b\n" "$num" "${options[$i]}" >&2
printf " ${CYAN}└─${RESET}\n" >&2
done
printf "\n" >&2
printf " ${DIM}Enter choice [1-%d]:${RESET} " "$count" >&2
read -r choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "$count" ]; then
choice=1
fi
echo "$choice"
}
prompt_yes_no() {
local prompt="$1" default="${2:-y}" hint answer
[ "$default" = "y" ] && hint="Y/n" || hint="y/N"
printf " ${BR_CYAN}?${RESET} ${BOLD}%s${RESET} ${DIM}[%s]${RESET}: " "$prompt" "$hint"
read -r answer
[ -z "$answer" ] && answer="$default"
case "$answer" in
[Yy]*) return 0 ;;
*) return 1 ;;
esac
}
# ─────────────────────────────────────────────
# Animated Spinner
# ─────────────────────────────────────────────
spinner() {
local pid=$1 msg="$2"
local frames=('⣾' '⣽' '⣻' '⢿' '⡿' '⣟' '⣯' '⣷')
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r ${BR_CYAN}%s${RESET} %s" "${frames[$i]}" "$msg"
i=$(( (i + 1) % ${#frames[@]} ))
sleep 0.1
done
printf "\r ${BR_GREEN}✓${RESET} %s\n" "$msg"
}
generate_secret_key() {
python3 -c "import secrets; print(''.join(secrets.choice('abcdefghijklmnopqrstuvwxyz0123456789_-') for _ in range(50)))" 2>/dev/null || \
head -c 50 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 50
}
env_quote() {
python3 -c 'import shlex, sys; print(shlex.quote(sys.argv[1]))' "$1"
}
load_previous_project_name() {
if [ -f "$SCRIPT_DIR/.env" ]; then
PREVIOUS_PROJECT_SLUG=$(grep -m1 "^# .* — Environment Configuration" "$SCRIPT_DIR/.env" 2>/dev/null | sed "s/^# //; s/ — Environment Configuration//" || true)
fi
[ -z "$PREVIOUS_PROJECT_SLUG" ] && PREVIOUS_PROJECT_SLUG="base_project"
PREVIOUS_PROJECT_DASH="${PREVIOUS_PROJECT_SLUG//_/-}"
}
# ─────────────────────────────────────────────
# Banner
# ─────────────────────────────────────────────
show_banner() {
clear_screen
printf "\n"
printf "${BR_CYAN}${BOLD}"
printf " ____ ___ ____ ____\n"
printf " / __ )____ _________ / | / __ \\/ _/\n"
printf " / __ / __ \`/ ___/ _ \\ / /| | / /_/ // / \n"
printf " / /_/ / /_/ (__ ) __/ / ___ |/ ____// / \n"
printf "/_____/\\__,_/____/\\___/ /_/ |_/_/ /___/ \n"
printf "${RESET}\n"
box_top
box_header "Django Backend Setup Wizard"
box_sep
box_empty
box_line " Configure | Rename | Launch"
box_line " your project from one place."
box_empty
box_sep
local os_label=""
case "$OS_TYPE" in
linux) os_label="Linux" ;;
macos) os_label="macOS" ;;
windows) os_label="Windows (Git Bash)" ;;
*) os_label="Unknown OS" ;;
esac
box_line " System: ${os_label} | $(date '+%Y-%m-%d %H:%M')"
box_bottom
printf "\n"
printf " ${DIM}Press ${BOLD}Enter${RESET}${DIM} to begin setup...${RESET}"
read -r
}
# ═══════════════════════════════════════════════
# STEP 1 — Project Identity
# ═══════════════════════════════════════════════
step_project_name() {
step_header 1 "Project Identity"
print_info "Rename all 'base_project' references in your codebase."
print_info "Use lowercase letters, digits and underscores."
printf "\n"
while true; do
prompt_input "Project name" "$PREVIOUS_PROJECT_SLUG" PROJECT_SLUG
if [[ ! "$PROJECT_SLUG" =~ ^[a-z][a-z0-9_]*$ ]]; then
print_error "Invalid! Use lowercase + underscores, starting with a letter."
continue
fi
local dash="${PROJECT_SLUG//_/-}"
local title
title=$(echo "$PROJECT_SLUG" | sed 's/_/ /g' | sed 's/\b\(.\)/\u\1/g')
local cap
cap=$(echo "$PROJECT_SLUG" | sed 's/_/ /g' | sed 's/^./\U&/')
printf "\n"
printf " ${DIM}Replacements:${RESET}\n"
printf " ${CYAN}base_project${RESET} → ${BR_GREEN}%s${RESET}\n" "$PROJECT_SLUG"
printf " ${CYAN}base-project${RESET} → ${BR_GREEN}%s${RESET}\n" "$dash"
printf " ${CYAN}Base Project${RESET} → ${BR_GREEN}%s${RESET}\n" "$title"
printf " ${CYAN}Base project${RESET} → ${BR_GREEN}%s${RESET}\n" "$cap"
printf "\n"
if prompt_yes_no "Confirm this name?"; then
PROJECT_NAME="$title"
break
fi
done
print_success "Project: ${PROJECT_SLUG}"
sleep 0.3
}
# ═══════════════════════════════════════════════
# STEP 2 — Run Mode
# ═══════════════════════════════════════════════
step_run_mode() {
step_header 2 "Run Mode"
local choice
choice=$(prompt_choice "How will you run this project?" \
"Docker Compose ${DIM}— containerized, recommended${RESET}" \
"Manual Setup ${DIM}— local venv, install yourself${RESET}")
case $choice in
1) RUN_MODE="docker" ;;
2) RUN_MODE="manual" ;;
esac
printf "\n"
print_success "Mode: ${RUN_MODE}"
sleep 0.3
}
# ═══════════════════════════════════════════════
# STEP 3 — Database
# ═══════════════════════════════════════════════
step_database() {
step_header 3 "Database"
local choice
if [ "$RUN_MODE" = "docker" ]; then
choice=$(prompt_choice "Database engine?" \
"PostgreSQL ${DIM}— production-ready, runs in container${RESET}" \
"SQLite ${DIM}— lightweight, file-based${RESET}")
else
choice=$(prompt_choice "Database engine?" \
"PostgreSQL ${DIM}— requires local PostgreSQL install${RESET}" \
"SQLite ${DIM}— zero setup, perfect for dev${RESET}")
fi
case $choice in
1) DB_CHOICE="postgresql" ;;
2) DB_CHOICE="sqlite" ;;
esac
if [ "$DB_CHOICE" = "postgresql" ]; then
printf "\n"
print_info "PostgreSQL credentials:"
printf "\n"
prompt_input "Database name" "${PROJECT_SLUG}_db" DB_NAME
prompt_input "Database user" "${PROJECT_SLUG}_user" DB_USER
prompt_password "Database password" "strong_password_123" DB_PASSWORD
if [ "$RUN_MODE" = "manual" ]; then
prompt_input "Host" "localhost" DB_HOST
prompt_input "Port" "5432" DB_PORT
else
DB_HOST="db_postgres"
DB_PORT="5432"
fi
else
DB_NAME="${PROJECT_SLUG}_db"
DB_USER="${PROJECT_SLUG}_user"
DB_PASSWORD="strong_password_123"
DB_HOST="localhost"
fi
printf "\n"
print_success "Database: ${DB_CHOICE}"
sleep 0.3
}
# ═══════════════════════════════════════════════
# STEP 4 — Services
# ═══════════════════════════════════════════════
step_services() {
step_header 4 "Optional Services"
print_info "Select additional services:"
printf "\n"
if prompt_yes_no "Enable MinIO (S3-compatible file storage)" "n"; then
USE_MINIO="true"
print_success "MinIO: ON"
else
printf " ${DIM} MinIO: OFF — local file storage${RESET}\n"
fi
printf "\n"
if prompt_yes_no "Enable Redis (cache, sessions & required for Celery)?" "n"; then
USE_REDIS="true"
print_success "Redis: ON"
printf "\n"
if prompt_yes_no "Enable Celery (background tasks)? Requires Redis." "n"; then
USE_CELERY="true"
print_success "Celery: ON"
else
USE_CELERY="false"
printf " ${DIM} Celery: OFF${RESET}\n"
fi
else
USE_CELERY="false"
printf " ${DIM} Redis: OFF${RESET}\n"
printf " ${DIM} Celery: OFF (Requires Redis)${RESET}\n"
fi
printf "\n"
sleep 0.3
}
# ═══════════════════════════════════════════════
# STEP 5 — Superuser
# ═══════════════════════════════════════════════
step_superuser() {
step_header 5 "Admin Account"
print_info "Django admin superuser credentials:"
printf "\n"
prompt_input "Username" "admin" ADMIN_USERNAME
prompt_input "Email" "admin@example.com" ADMIN_EMAIL
prompt_password "Password" "adminpass123" ADMIN_PASSWORD
if [ ${#ADMIN_PASSWORD} -lt 8 ]; then
print_warning "Short password (< 8 chars). Consider a stronger one."
fi
printf "\n"
print_success "Admin: ${ADMIN_USERNAME} (${ADMIN_EMAIL})"
sleep 0.3
}
# ═══════════════════════════════════════════════
# STEP 6 — Summary
# ═══════════════════════════════════════════════
step_summary() {
step_header 6 "Review & Confirm"
local minio_label="OFF"
local redis_label="OFF"
[ "$USE_MINIO" = "true" ] && minio_label="${BR_GREEN}ON${RESET}"
[ "$USE_REDIS" = "true" ] && redis_label="${BR_GREEN}ON${RESET}"
printf " ${BOLD}Configuration Summary:${RESET}\n\n"
printf " %-18s ${BR_WHITE}%s${RESET}\n" "Project:" "$PROJECT_SLUG"
printf " %-18s ${BR_WHITE}%s${RESET}\n" "Mode:" "$RUN_MODE"
printf " %-18s ${BR_WHITE}%s${RESET}\n" "Database:" "$DB_CHOICE"
if [ "$DB_CHOICE" = "postgresql" ]; then
printf " %-18s ${BR_WHITE}%s${RESET}\n" " DB Name:" "$DB_NAME"
printf " %-18s ${BR_WHITE}%s${RESET}\n" " DB User:" "$DB_USER"
printf " %-18s ${BR_WHITE}%s${RESET}\n" " DB Host:" "$DB_HOST"
fi
printf " %-18s %b\n" "MinIO:" "$minio_label"
printf " %-18s %b\n" "Redis:" "$redis_label"
printf " %-18s ${BR_WHITE}%s${RESET}\n" "Admin:" "$ADMIN_USERNAME"
printf " %-18s ${BR_WHITE}%s${RESET}\n" "Email:" "$ADMIN_EMAIL"
printf "\n"
if ! prompt_yes_no "Apply this configuration?"; then
print_warning "Cancelled. No changes made."
exit 0
fi
}
# ═══════════════════════════════════════════════
# Apply Changes
# ═══════════════════════════════════════════════
apply_generate_env() {
print_arrow "Generating .env ..."
SECRET_KEY=$(generate_secret_key)
local use_sqlite="False"
local use_minio_val="False"
local db_host_val="${DB_HOST:-localhost}"
[ "$DB_CHOICE" = "sqlite" ] && use_sqlite="True"
[ "$USE_MINIO" = "true" ] && use_minio_val="True"
local dash="${PROJECT_SLUG//_/-}"
local quoted_secret_key quoted_db_password quoted_admin_password
quoted_secret_key=$(env_quote "$SECRET_KEY")
quoted_db_password=$(env_quote "${DB_PASSWORD:-strong_password_123}")
quoted_admin_password=$(env_quote "$ADMIN_PASSWORD")
local compose_profiles=""
[ "$DB_CHOICE" = "postgresql" ] && compose_profiles="${compose_profiles}postgres,"
[ "$USE_MINIO" = "true" ] && compose_profiles="${compose_profiles}minio,"
[ "$USE_REDIS" = "true" ] && compose_profiles="${compose_profiles}redis,"
[ "$USE_CELERY" = "true" ] && compose_profiles="${compose_profiles}celery,"
# Remove trailing comma
compose_profiles="${compose_profiles%,}"
cat > "$SCRIPT_DIR/.env" << ENVEOF
# ==============================================================================
# ${PROJECT_SLUG} — Environment Configuration
# Generated by start.sh on $(date '+%Y-%m-%d %H:%M:%S')
# ==============================================================================
# --- Docker Compose Profiles ---
COMPOSE_PROFILES=${compose_profiles}
# --- Django ---
SECRET_KEY=${quoted_secret_key}
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0
CSRF_TRUSTED_ORIGINS=http://localhost,http://127.0.0.1
ALLOWED_CORS=http://localhost:3000,http://127.0.0.1:3000
# --- Database ---
USE_SQLITE=${use_sqlite}
DB_NAME=${DB_NAME:-${PROJECT_SLUG}_db}
DB_USER=${DB_USER:-${PROJECT_SLUG}_user}
DB_PASSWORD=${quoted_db_password}
DB_HOST=${db_host_val}
DB_PORT=${DB_PORT:-5432}
# --- Superuser ---
CREATE_SUPERUSER=True
SUPERUSER_USERNAME=${ADMIN_USERNAME}
SUPERUSER_EMAIL=${ADMIN_EMAIL}
SUPERUSER_PASSWORD=${quoted_admin_password}
# --- MinIO / S3 ---
USE_MINIO=${use_minio_val}
AWS_ACCESS_KEY_ID=minio_admin
AWS_SECRET_ACCESS_KEY='minio_password'
AWS_STORAGE_BUCKET_NAME=${dash}-media
AWS_S3_ENDPOINT_URL=http://minio:9000
AWS_S3_MINIO_ENDPOINT_URL=http://localhost:9000
AWS_S3_USE_SSL=False
AWS_QUERYSTRING_AUTH=True
AWS_S3_CUSTOM_DOMAIN=localhost:9000/${dash}-media
AWS_S3_URL_PROTOCOL=http:
AWS_DEFAULT_ACL=private
AWS_S3_OBJECT_PARAMETERS={"CacheControl": "max-age=86400"}
# --- JWT ---
JWT_ACCESS_LIFETIME_MINUTES=60
JWT_REFRESH_LIFETIME_DAYS=7
# --- Payments ---
NOWPAYMENTS_API_KEY=
NOWPAYMENTS_SANDBOX_API_KEY=
ZARINPAL_MERCHANT_ID=
CALLBACK_URL=https://example.com/payment/verify/
CANCEL_URL=https://example.com/payment/cancel/
# --- Misc ---
ENABLE_FIELD_FILTER_PAGINATION=False
ENVEOF
print_success ".env generated"
}
save_project_history() {
local history_key="PROJECT_HISTORY"
local current_name="$PROJECT_SLUG"
if [ ! -f "$SCRIPT_DIR/.env" ]; then
touch "$SCRIPT_DIR/.env"
fi
local current_history
current_history=$(grep "^${history_key}=" "$SCRIPT_DIR/.env" 2>/dev/null | cut -d'=' -f2- || true)
if [ -z "$current_history" ]; then
current_history="base_project"
fi
if ! echo "$current_history" | grep -q "\b${current_name}\b"; then
current_history="${current_history},${current_name}"
fi
if [ "$OS_TYPE" = "macos" ]; then
sed -i '' "s/^${history_key}=.*/${history_key}=${current_history}/" "$SCRIPT_DIR/.env" 2>/dev/null || \
echo "${history_key}=${current_history}" >> "$SCRIPT_DIR/.env"
else
sed -i "s/^${history_key}=.*/${history_key}=${current_history}/" "$SCRIPT_DIR/.env" 2>/dev/null || \
echo "${history_key}=${current_history}" >> "$SCRIPT_DIR/.env"
fi
}
load_project_history() {
local history_key="PROJECT_HISTORY"
local old_names=()
if [ -f "$SCRIPT_DIR/.env" ]; then
local history
history=$(grep "^${history_key}=" "$SCRIPT_DIR/.env" 2>/dev/null | cut -d'=' -f2- || true)
if [ -n "$history" ]; then
IFS=',' read -ra old_names <<< "$history"
fi
fi
if [ ${#old_names[@]} -eq 0 ]; then
old_names=("${PREVIOUS_PROJECT_SLUG:-base_project}")
fi
echo "${old_names[@]}"
}
apply_rename() {
print_arrow "Renaming project files..."
local new_slug="$PROJECT_SLUG"
local new_dash="${PROJECT_SLUG//_/-}"
local new_title
new_title=$(echo "$PROJECT_SLUG" | sed 's/_/ /g' | sed 's/\b\(.\)/\u\1/g')
local new_cap
new_cap=$(echo "$PROJECT_SLUG" | sed 's/_/ /g' | sed 's/^./\U&/')
local old_names=($(load_project_history))
local all_names=("${old_names[@]}" "$new_slug")
local unique_names=()
for name in "${all_names[@]}"; do
local found=false
for uniq in "${unique_names[@]}"; do
if [ "$name" = "$uniq" ]; then
found=true
break
fi
done
if [ "$found" = false ]; then
unique_names+=("$name")
fi
done
local files=("docker-compose.yml" "config/settings.py" "config/urls.py" ".env" "scripts/entrypoint.sh" "config/celery.py")
local count=0
for file in "${files[@]}"; do
local fp="$SCRIPT_DIR/$file"
[ ! -f "$fp" ] && continue
local changed=false
for old_slug in "${unique_names[@]}"; do
[ "$old_slug" = "$new_slug" ] && continue
local old_dash="${old_slug//_/-}"
local old_title
old_title=$(echo "$old_slug" | sed 's/_/ /g' | sed 's/\b\(.\)/\u\1/g')
local old_cap
old_cap=$(echo "$old_slug" | sed 's/_/ /g' | sed 's/^./\U&/')
local replacements=(
"${old_slug}:${new_slug}"
"${old_dash}:${new_dash}"
"${old_title}:${new_title}"
"${old_cap}:${new_cap}"
)
for pair in "${replacements[@]}"; do
local from="${pair%%:*}"
local to="${pair#*:}"
[ "$from" = "$to" ] && continue
if grep -q "$from" "$fp" 2>/dev/null; then
if [ "$OS_TYPE" = "macos" ]; then
sed -i '' "s/${from}/${to}/g" "$fp"
else
sed -i "s/${from}/${to}/g" "$fp"
fi
changed=true
fi
done
done
if [ "$changed" = true ]; then
print_success "Updated: ${file}"
count=$((count + 1))
fi
done
[ $count -eq 0 ] && print_info "No files needed updating."
save_project_history
}
warn_existing_postgres_data() {
if [ "$RUN_MODE" != "docker" ] || [ "$DB_CHOICE" != "postgresql" ]; then
return
fi
if [ -d "$SCRIPT_DIR/postgres_data" ]; then
printf "\n"
print_warning "Existing postgres_data directory found."
print_warning "If DB_NAME, DB_USER or DB_PASSWORD changed, PostgreSQL will keep the old database roles."
print_info "For a clean local reset, stop Docker and remove postgres_data manually:"
print_info " docker compose down"
print_info " rmdir /s /q postgres_data # Windows CMD"
print_info " Remove-Item -Recurse -Force .\\postgres_data # PowerShell"
printf "\n"
fi
}
apply_finish() {
printf "\n"
box_top
box_header "Setup Complete!"
box_sep
box_empty
if [ "$RUN_MODE" = "docker" ]; then
box_line " Start your project:"
box_empty
box_line " docker compose up --build -d"
box_empty
if [ "$PROJECT_SLUG" != "base_project" ]; then
box_sep
box_line " Rename folder (optional):"
box_line " mv $(basename "$SCRIPT_DIR") ${PROJECT_SLUG}_api"
fi
else
box_line " Start your project:"
box_empty
box_line " python -m venv venv"
if [ "$OS_TYPE" = "windows" ]; then
box_line " venv\\Scripts\\activate"
else
box_line " source venv/bin/activate"
fi
box_line " pip install -r requirements/local.txt"
box_line " python manage.py migrate"
box_line " python manage.py createsuperuser"
box_line " python manage.py runserver"
fi
box_empty
box_bottom
printf "\n"
if [ "$RUN_MODE" = "docker" ]; then
if prompt_yes_no "Launch Docker Compose now?" "y"; then
printf "\n"
print_arrow "Starting containers..."
printf "\n"
printf " ${DIM}$ docker compose up --build -d${RESET}\n\n"
cd "$SCRIPT_DIR"
docker compose up --build -d
else
printf "\n"
print_info "Run the command above when you're ready."
fi
fi
printf "\n"
printf " ${BR_CYAN}${BOLD}Happy coding!${RESET} ${DIM}🎉${RESET}\n\n"
}
# ═══════════════════════════════════════════════
# Main
# ═══════════════════════════════════════════════
main() {
detect_os
setup_colors
load_previous_project_name
if [ ! -f "$SCRIPT_DIR/manage.py" ]; then
print_error "manage.py not found! Run from project root."
exit 1
fi
show_banner
step_project_name
step_run_mode
step_database
step_services
step_superuser
step_summary
# Apply
clear_screen
printf "\n"
box_top
box_header "Applying Configuration..."
box_bottom
printf "\n"
apply_generate_env
apply_rename
warn_existing_postgres_data
apply_finish
}
main "$@"