172 lines
3.5 KiB
Bash
Executable File
172 lines
3.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
source /opt/appfactory/config/appfactory.env
|
|
|
|
APP_ID="${1:-}"
|
|
APP_NAME="${2:-$APP_ID}"
|
|
|
|
if [ -z "$APP_ID" ]; then
|
|
echo "Usage: new-nodejs-ts-app.sh <app-id> [app-name]"
|
|
exit 1
|
|
fi
|
|
|
|
APP_DIR="$WORKSPACE_DIR/$APP_ID"
|
|
TARGET_DIR="$APP_DIR"
|
|
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
|
|
|
|
mkdir -p "$APP_DIR/src"
|
|
cd "$APP_DIR"
|
|
|
|
cat > package.json <<EOF_JSON
|
|
{
|
|
"name": "$APP_ID",
|
|
"version": "1.0.0",
|
|
"scripts": {
|
|
"build": "tsc",
|
|
"start": "node dist/index.js"
|
|
},
|
|
"dependencies": {
|
|
"express": "^4.18.3"
|
|
},
|
|
"devDependencies": {
|
|
"@types/express": "^4.17.21",
|
|
"@types/node": "^20.11.30",
|
|
"typescript": "^5.4.0"
|
|
}
|
|
}
|
|
EOF_JSON
|
|
|
|
cat > tsconfig.json <<'EOF_JSON'
|
|
{
|
|
"compilerOptions": {
|
|
"target": "ES2022",
|
|
"module": "CommonJS",
|
|
"outDir": "dist",
|
|
"rootDir": "src",
|
|
"strict": true,
|
|
"esModuleInterop": true
|
|
}
|
|
}
|
|
EOF_JSON
|
|
|
|
cat > src/index.ts <<EOF_TS
|
|
import express from "express";
|
|
|
|
const app = express();
|
|
const port = Number(process.env.PORT || 3000);
|
|
const rootPath = process.env.ROOT_PATH || "";
|
|
|
|
app.get("/", (_req, res) => {
|
|
res.json({
|
|
name: "$APP_NAME",
|
|
service: "$APP_ID",
|
|
status: "ok"
|
|
});
|
|
});
|
|
|
|
app.get("/health", (_req, res) => {
|
|
res.json({ status: "ok" });
|
|
});
|
|
|
|
if (rootPath) {
|
|
app.get(rootPath, (_req, res) => {
|
|
res.json({
|
|
name: "$APP_NAME",
|
|
service: "$APP_ID",
|
|
status: "ok"
|
|
});
|
|
});
|
|
|
|
app.get(rootPath + "/health", (_req, res) => {
|
|
res.json({ status: "ok" });
|
|
});
|
|
}
|
|
|
|
app.listen(port, "0.0.0.0", () => {
|
|
console.log("$APP_ID listening on port " + port);
|
|
});
|
|
EOF_TS
|
|
|
|
cat > Dockerfile <<'EOF_DOCKER'
|
|
FROM node:20-slim AS build
|
|
WORKDIR /app
|
|
COPY package*.json ./
|
|
RUN npm install
|
|
COPY . .
|
|
RUN npm run build
|
|
|
|
FROM node:20-slim
|
|
WORKDIR /app
|
|
ENV PORT=3000
|
|
EXPOSE 3000
|
|
COPY package*.json ./
|
|
RUN npm install --omit=dev
|
|
COPY --from=build /app/dist ./dist
|
|
CMD ["npm", "start"]
|
|
EOF_DOCKER
|
|
|
|
cat > .dockerignore <<'EOF_IGNORE'
|
|
node_modules/
|
|
dist/
|
|
.git/
|
|
EOF_IGNORE
|
|
|
|
cat > README.md <<EOF_MD
|
|
# $APP_NAME
|
|
|
|
Node.js TypeScript služba vytvořená přes CSBot Services Portal.
|
|
|
|
## Endpointy
|
|
|
|
- GET /
|
|
- GET /health
|
|
EOF_MD
|
|
|
|
cp "$TOOLS_DIR/AGENTS.md" "$TARGET_DIR/AGENTS.md"
|
|
|
|
git init
|
|
git config user.name "AppFactory Bot"
|
|
git config user.email "appfactory@local"
|
|
git branch -M main
|
|
git add .
|
|
git commit -m "Initial Node.js TypeScript service"
|
|
|
|
curl -sS -X POST "$GITEA_URL/api/v1/orgs/$GITEA_ORG/repos" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"name\":\"$APP_ID\",\"private\":false,\"auto_init\":false}" >/dev/null || true
|
|
|
|
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
|
|
|
|
if [[ "$GITEA_URL" == http://* ]]; then
|
|
GITEA_PUSH_URL="${GITEA_URL/http:\/\//http:\/\/${GITEA_TOKEN}@}"
|
|
elif [[ "$GITEA_URL" == https://* ]]; then
|
|
GITEA_PUSH_URL="${GITEA_URL/https:\/\//https:\/\/${GITEA_TOKEN}@}"
|
|
else
|
|
echo "Unsupported GITEA_URL: $GITEA_URL"
|
|
exit 1
|
|
fi
|
|
git remote remove origin 2>/dev/null || true
|
|
git remote add origin "$GITEA_PUSH_URL/$GITEA_ORG/$APP_ID.git"
|
|
git push -u origin main
|