130 lines
3.1 KiB
Bash
Executable File
130 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
|
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
|
die() { echo -e "${RED}[x] FATAL:${NC} $*" >&2; exit 1; }
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
BACKEND_DIR="$ROOT_DIR/backend"
|
|
FRONTEND_DIR="$ROOT_DIR/frontend"
|
|
ENV_FILE="$BACKEND_DIR/.env"
|
|
|
|
require_command() {
|
|
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
|
|
}
|
|
|
|
ensure_uv() {
|
|
if command -v uv >/dev/null 2>&1; then
|
|
return
|
|
fi
|
|
|
|
info "Installing uv..."
|
|
require_command curl
|
|
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
export PATH="$HOME/.local/bin:$PATH"
|
|
command -v uv >/dev/null 2>&1 || die "uv installed but not found in PATH"
|
|
}
|
|
|
|
ensure_python() {
|
|
info "Ensuring Python 3.14 is available via uv..."
|
|
uv python install 3.14
|
|
}
|
|
|
|
sync_env_values() {
|
|
local mongo_pass mongo_db
|
|
mongo_pass=$(grep -E '^MONGO_INITDB_ROOT_PASSWORD=' "$ENV_FILE" | cut -d '=' -f2-)
|
|
mongo_db=$(grep -E '^MONGODB_DB_NAME=' "$ENV_FILE" | cut -d '=' -f2-)
|
|
|
|
[[ -n "$mongo_db" ]] || mongo_db="theplug"
|
|
|
|
if [[ -n "$mongo_pass" ]]; then
|
|
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$mongo_pass@localhost:27017/$mongo_db?authSource=admin|" "$ENV_FILE"
|
|
fi
|
|
}
|
|
|
|
ensure_env() {
|
|
if [[ -f "$ENV_FILE" ]]; then
|
|
sync_env_values
|
|
return
|
|
fi
|
|
|
|
warn "backend/.env not found, creating it from backend/.env.example"
|
|
cp "$BACKEND_DIR/.env.example" "$ENV_FILE"
|
|
|
|
local mongo_pass jwt_secret auth_pass
|
|
mongo_pass=$(openssl rand -hex 8)
|
|
jwt_secret=$(openssl rand -hex 20)
|
|
auth_pass=$(openssl rand -hex 10)
|
|
|
|
sed -i "s|^MONGO_INITDB_ROOT_PASSWORD=.*|MONGO_INITDB_ROOT_PASSWORD=$mongo_pass|" "$ENV_FILE"
|
|
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$mongo_pass@localhost:27017/theplug?authSource=admin|" "$ENV_FILE"
|
|
sed -i "s|^JWT_SECRET_KEY=.*|JWT_SECRET_KEY=$jwt_secret|" "$ENV_FILE"
|
|
sed -i "s|^AUTH_PASSWORD=.*|AUTH_PASSWORD=$auth_pass|" "$ENV_FILE"
|
|
|
|
info "Generated credentials in $ENV_FILE"
|
|
info "AUTH_USERNAME: admin"
|
|
info "AUTH_PASSWORD: $auth_pass"
|
|
}
|
|
|
|
prepare_backend() {
|
|
info "Installing backend dependencies..."
|
|
(
|
|
cd "$BACKEND_DIR"
|
|
uv sync --python 3.14
|
|
)
|
|
}
|
|
|
|
prepare_frontend() {
|
|
require_command npm
|
|
info "Installing frontend dependencies..."
|
|
(
|
|
cd "$FRONTEND_DIR"
|
|
npm install
|
|
npm run build
|
|
)
|
|
}
|
|
|
|
prepare_docker_assets() {
|
|
require_command docker
|
|
info "Preparing Docker assets..."
|
|
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/docker-compose.yml" pull mongodb
|
|
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/docker-compose.yml" build frontend
|
|
}
|
|
|
|
print_next_steps() {
|
|
cat <<EOF
|
|
|
|
Setup complete.
|
|
|
|
Next commands:
|
|
./run.sh mongo up
|
|
./run.sh backend
|
|
./run.sh frontend up
|
|
|
|
Optional:
|
|
./dev.sh mongo up
|
|
./dev.sh backend
|
|
./dev.sh frontend
|
|
./dev.sh services list
|
|
EOF
|
|
}
|
|
|
|
main() {
|
|
require_command openssl
|
|
ensure_uv
|
|
ensure_python
|
|
ensure_env
|
|
prepare_backend
|
|
prepare_frontend
|
|
prepare_docker_assets
|
|
print_next_steps
|
|
}
|
|
|
|
main "$@"
|