Files
minxa.lol/generate_configs.py

117 lines
3.0 KiB
Python

import secrets
import random
from pathlib import Path
# === Prompt user input with defaults === #
react_api_url = input("🔧 Enter the REACT_APP_API_BASE_URL (default: http://localhost:8000): ").strip()
if not react_api_url:
react_api_url = "http://localhost:8000"
allow_origins = input("🔒 Enter allowed CORS origins (comma-separated) (default: http://localhost:3000,http://127.0.0.1:3000): ").strip()
if not allow_origins:
allow_origins = "http://localhost:3000,http://127.0.0.1:3000"
# Ask for environment and validate input
valid_envs = ["dev", "stage", "prod"]
while True:
environment = input("🌎 Enter environment (dev, stage, prod): ").strip().lower()
if environment in valid_envs:
break
print("❌ Invalid input. Please enter one of: dev, stage, prod")
# === Backend values === #
db_user = "minxa"
db_password = secrets.token_urlsafe(16)
db_name = "minxadb"
db_host = "db"
db_port = 5432
hashids_salt = secrets.token_urlsafe(32)
alphabet = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
random.shuffle(alphabet)
encoder_alphabet = "".join(alphabet)
# === Generate backend .env === #
backend_env = f"""# Auto-generated backend .env
DATABASE_URL=postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}
ENVIRONMENT={environment}
HASHIDS_SALT={hashids_salt}
ENCODER_ALPHABET={encoder_alphabet}
ALLOW_ORIGINS={allow_origins}
"""
backend_env_path = Path("backend/.env")
backend_env_path.parent.mkdir(parents=True, exist_ok=True)
backend_env_path.write_text(backend_env)
print(f"✅ backend/.env written")
# === Generate frontend .env === #
frontend_env = f"""# Auto-generated frontend .env
REACT_APP_API_BASE_URL={react_api_url}
"""
frontend_env_path = Path("frontend/.env")
frontend_env_path.parent.mkdir(parents=True, exist_ok=True)
frontend_env_path.write_text(frontend_env)
print(f"✅ frontend/.env written")
# === Generate docker-compose.generated.yml === #
compose_yml = f"""services:
db:
image: postgres:16
environment:
POSTGRES_USER: {db_user}
POSTGRES_PASSWORD: {db_password}
POSTGRES_DB: {db_name}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "{db_user}", "-d", "{db_name}"]
interval: 5s
timeout: 5s
retries: 5
networks:
- appnet
backend:
build:
context: .
dockerfile: backend/Dockerfile
env_file:
- ./backend/.env
depends_on:
db:
condition: service_healthy
ports:
- "8000:8000"
networks:
- appnet
frontend:
build:
context: ./frontend
env_file:
- ./frontend/.env
depends_on:
- backend
ports:
- "3000:80"
networks:
- appnet
volumes:
postgres_data:
networks:
appnet:
"""
compose_path = Path("docker-compose.generated.yml")
compose_path.write_text(compose_yml)
print(f"✅ docker-compose.generated.yml written")
print("\n🎉 All files generated! Run your stack with:\n")
print(" docker compose -f docker-compose.generated.yml up --build -d")