#!/usr/bin/env bash set -euo pipefail CONFIG_FILE="/opt/appfactory/config/appfactory.env" source "$CONFIG_FILE" APP_ID="${1:-}" APP_NAME="${2:-$APP_ID}" if [ -z "$APP_ID" ]; then echo "Usage: ./new-python-app.sh [app-name]" exit 1 fi TEMPLATE_DIR="$WORKSPACE_DIR/python-fastapi-template" TARGET_DIR="$WORKSPACE_DIR/$APP_ID" if [ ! -d "$TEMPLATE_DIR" ]; then echo "Template not found: $TEMPLATE_DIR" exit 1 fi if [ -d "$TARGET_DIR" ]; then echo "Target already exists: $TARGET_DIR" exit 1 fi echo "Creating app from template..." cp -R "$TEMPLATE_DIR" "$TARGET_DIR" cd "$TARGET_DIR" rm -rf .git sed -i "s/Python FastAPI Template/$APP_NAME/g" app/main.py cat > app.yml < README.md < /tmp/appfactory-create-repo-response.json if grep -q '"message"' /tmp/appfactory-create-repo-response.json; then cat /tmp/appfactory-create-repo-response.json fi echo "Creating Gitea webhook..." curl -sS -X POST \ "$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP_ID/hooks" \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "{ \"type\": \"gitea\", \"active\": true, \"events\": [\"push\"], \"config\": { \"url\": \"$WEBHOOK_INTERNAL_URL\", \"content_type\": \"json\", \"secret\": \"$WEBHOOK_SECRET\" } }" > /tmp/appfactory-create-hook-response.json if grep -q '"message"' /tmp/appfactory-create-hook-response.json; then cat /tmp/appfactory-create-hook-response.json fi git init git config user.name "AppFactory Bot" git config user.email "appfactory@local" git branch -M main GITEA_PUSH_URL="${GITEA_URL/http:\/\//http:\/\/${GITEA_TOKEN}@}" git remote add origin "$GITEA_PUSH_URL/$GITEA_ORG/$APP_ID.git" git add . git commit -m "Initial $APP_NAME" git push -u origin main chown -R "$APP_USER:$APP_GROUP" "$TARGET_DIR" || true echo "" echo "DONE" echo "Repository:" echo "$GITEA_URL/$GITEA_ORG/$APP_ID" echo "" echo "Local path:" echo "$TARGET_DIR"