201 lines
4.4 KiB
Bash
Executable File
201 lines
4.4 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-dotnet-worker-app.sh <app-id> [app-name]"
|
|
exit 1
|
|
fi
|
|
|
|
APP_DIR="$WORKSPACE_DIR/$APP_ID"
|
|
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
|
|
PROJECT_NAME="$(python3 - "$APP_ID" <<'PY'
|
|
import re
|
|
import sys
|
|
|
|
parts = re.split(r'[^a-zA-Z0-9]+', sys.argv[1])
|
|
print(''.join(p[:1].upper() + p[1:] for p in parts if p))
|
|
PY
|
|
)"
|
|
|
|
mkdir -p "$APP_DIR"
|
|
cd "$APP_DIR"
|
|
|
|
cat > "$PROJECT_NAME.csproj" <<EOF_DOTNET
|
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
|
<PropertyGroup>
|
|
<TargetFramework>net8.0</TargetFramework>
|
|
<Nullable>enable</Nullable>
|
|
<ImplicitUsings>enable</ImplicitUsings>
|
|
</PropertyGroup>
|
|
|
|
<ItemGroup>
|
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
|
|
</ItemGroup>
|
|
</Project>
|
|
EOF_DOTNET
|
|
|
|
cat > Program.cs <<EOF_DOTNET
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddHostedService<Worker>();
|
|
|
|
var app = builder.Build();
|
|
|
|
var rootPath = Environment.GetEnvironmentVariable("ROOT_PATH");
|
|
if (!string.IsNullOrWhiteSpace(rootPath))
|
|
{
|
|
app.UsePathBase(rootPath);
|
|
}
|
|
|
|
app.MapGet("/", () => Results.Json(new
|
|
{
|
|
name = "$APP_NAME",
|
|
service = "$APP_ID",
|
|
type = "worker",
|
|
status = "ok"
|
|
}));
|
|
|
|
app.MapGet("/health", () => Results.Json(new
|
|
{
|
|
status = "ok"
|
|
}));
|
|
|
|
app.Run();
|
|
|
|
public sealed class Worker : BackgroundService
|
|
{
|
|
private readonly ILogger<Worker> _logger;
|
|
|
|
public Worker(ILogger<Worker> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("Service $APP_ID started.");
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
_logger.LogInformation("Service $APP_ID is running at {time}", DateTimeOffset.Now);
|
|
await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken);
|
|
}
|
|
|
|
_logger.LogInformation("Service $APP_ID stopped.");
|
|
}
|
|
}
|
|
EOF_DOTNET
|
|
|
|
cat > appsettings.json <<'EOF_JSON'
|
|
{
|
|
"Logging": {
|
|
"LogLevel": {
|
|
"Default": "Information",
|
|
"Microsoft.Hosting.Lifetime": "Information"
|
|
}
|
|
}
|
|
}
|
|
EOF_JSON
|
|
|
|
cat > Dockerfile <<EOF_DOCKER
|
|
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
|
WORKDIR /src
|
|
COPY . .
|
|
RUN dotnet publish "$PROJECT_NAME.csproj" -c Release -o /app/publish
|
|
|
|
FROM mcr.microsoft.com/dotnet/aspnet:8.0
|
|
WORKDIR /app
|
|
ENV ASPNETCORE_URLS=http://+:8080
|
|
EXPOSE 8080
|
|
COPY --from=build /app/publish .
|
|
ENTRYPOINT ["dotnet", "$PROJECT_NAME.dll"]
|
|
EOF_DOCKER
|
|
|
|
cat > .dockerignore <<'EOF_IGNORE'
|
|
bin/
|
|
obj/
|
|
.git/
|
|
EOF_IGNORE
|
|
|
|
cat > README.md <<EOF_MD
|
|
# $APP_NAME
|
|
|
|
.NET Worker služba vytvořená přes CSBot Services Portal.
|
|
|
|
Obsahuje background worker a HTTP health endpoint.
|
|
|
|
## Endpointy
|
|
|
|
- GET /
|
|
- GET /health
|
|
|
|
## Spuštění lokálně
|
|
|
|
\`\`\`bash
|
|
dotnet run
|
|
\`\`\`
|
|
|
|
## Docker
|
|
|
|
\`\`\`bash
|
|
docker build -t $APP_ID:latest .
|
|
docker run --rm -p 8080:8080 $APP_ID:latest
|
|
\`\`\`
|
|
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 .NET Worker 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
|