From d3a362decd1d88b0dd40fc9c5efc3999844b2577 Mon Sep 17 00:00:00 2001 From: ReLoia Date: Mon, 13 Apr 2026 01:09:12 +0200 Subject: [PATCH] feat(setup): add setup.sh script for automated environment provisioning and app launch --- .env.example | 10 +++---- README.md | 8 +++++- app/main.py | 7 +++++ setup.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 setup.sh diff --git a/.env.example b/.env.example index 85d0759..5520098 100644 --- a/.env.example +++ b/.env.example @@ -1,10 +1,10 @@ API_PORT=3671 -MONGO_INITDB_ROOT_PASSWORD=pass -MONGODB_URI=mongodb://theplug:pass@mongodb:27017/theplug?authSource=admin +MONGO_INITDB_ROOT_PASSWORD=pass# esegui 'openssl rand -hex 5' in un terminale e copia qui +MONGODB_URI=mongodb://theplug:pass@localhost:27017/theplug?authSource=admin MONGODB_DB_NAME=theplug -REDIS_URL=redis://redis:6379 -JWT_SECRET_KEY=change-this-secret +REDIS_URL=redis://localhost:6379 +JWT_SECRET_KEY= # esegui 'openssl rand -hex 20' in un terminale e copia qui JWT_ALGORITHM=HS256 JWT_EXPIRE_MINUTES=30 AUTH_USERNAME=admin -AUTH_PASSWORD=change-this-password \ No newline at end of file +AUTH_PASSWORD= # esegui 'openssl rand -hex 20' in un terminale e copia qui \ No newline at end of file diff --git a/README.md b/README.md index 6682ed5..dc60476 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,13 @@ icona di una presa della spina idk FastAPI + MongoDB + Redis ``` -### Setup Dev +## Quick Setup (prod) + +```bash +bash <(curl -s https://git.reloia.uk/reloia/thePlug/raw/branch/master/setup.sh) +``` + +## Setup Dev imposta le variabili d'ambiente diff --git a/app/main.py b/app/main.py index 0494a77..fe1ef19 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,4 @@ +import os import sys from contextlib import asynccontextmanager from pathlib import Path @@ -43,9 +44,15 @@ async def lifespan(_: FastAPI): await close_redis() await close_mongodb() + +DEBUG = os.getenv("DEBUG", "false").lower() == "true" + app = FastAPI( title="thePlugAPI", lifespan=lifespan, + docs_url="/docs" if DEBUG else None, + redoc_url="/redoc" if DEBUG else None, + openapi_url="/openapi.json" if DEBUG else None, ) app.include_router(auth_router) diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..50b3db9 --- /dev/null +++ b/setup.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +set -euo pipefail + +# nota di Renato +# fatto da Claude Sonnet 4.6 (preferisco tagliarmi le palle che scrivere un bash a mano) + +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}[✗] FATAL:${NC} $*" >&2; exit 1; } + +# ── 1. ensure we're in thePlug ──────────────────────────────────────────────── +if [[ "$(basename "$PWD")" != "thePlug" ]]; then + warn "Not in thePlug — cloning..." + git clone https://git.reloia.uk/reloia/thePlug || die "git clone failed" + cd thePlug +fi + +# ── 2. spin up dependencies ─────────────────────────────────────────────────── +info "Starting Redis and MongoDB..." +docker compose up -d redis mongodb || die "docker compose failed — are Redis/MongoDB defined in compose.yml?" + +# ── 3. ensure uv is available ───────────────────────────────────────────────── +if ! command -v uv &>/dev/null; then + warn "uv not found — installing..." + curl -LsSf https://astral.sh/uv/install.sh | sh || die "uv installation failed" + export PATH="$HOME/.local/bin:$PATH" + command -v uv &>/dev/null || die "uv installed but still not in PATH — open a new shell and retry" + info "uv installed: $(uv --version)" +fi + +# ── 4. ensure tmux is available ─────────────────────────────────────────────── +if ! command -v tmux &>/dev/null; then + warn "tmux not found — installing..." + sudo apt-get install -y tmux || die "tmux installation failed" + info "tmux installed" +fi + +# ── 5. venv + deps ──────────────────────────────────────────────────────────── +info "Pinning Python 3.14..." +uv python pin 3.14 + +info "Creating/updating venv..." +uv venv +source .venv/bin/activate + +info "Syncing dependencies..." +uv sync + +# 6 + +if [[ ! -f .env ]]; then + warn ".env not found — copyinh from .env.example..." + cp .env.example .env || die ".env.example not found (impossibile)" + info "Opening the editor on the .env file (non vedrai mai questo messaggio)..." + ${EDITOR:-nano} .env +fi + +# ── - E DOPO C'È IL 7 !!!!. run in tmux ──────────────────────────────────────────────────────────── +SESSION="theplug" + +if tmux has-session -t "$SESSION" 2>/dev/null; then + warn "tmux session '$SESSION' already exists — killing it..." + tmux kill-session -t "$SESSION" +fi + +info "Launching FastAPI in tmux session '$SESSION'..." +API_PORT=$(grep -E '^API_PORT=' .env | cut -d '=' -f2) +API_PORT=${API_PORT:-3671} +tmux new-session -d -s "$SESSION" \ + "cd $(pwd) && source .venv/bin/activate && PYTHONPATH=app uv run uvicorn app.main:app --host 0.0.0.0 --port $API_PORT 2>&1 | tee uvicorn.log; echo 'PROCESS EXITED — press enter to close'; read" + +info "Done — attach with: tmux attach -t $SESSION" \ No newline at end of file