# Service Gateway Registration Script param( [string]$Prefix = "activity", [string]$Version = "v1", [string]$GatewayUrl = "http://localhost:5000", [string]$Address = "http://localhost:5001", [string]$DestinationId = $null, [int]$Weight = 1, [switch]$Global = $true, [string]$TenantCode = "" ) $ErrorActionPreference = "Stop" # Derived values $ClusterId = "$Prefix-service" if ([string]::IsNullOrEmpty($DestinationId)) { $DestinationId = "$Prefix-1" } $PathPattern = "/$Prefix/$Version/{**path}" Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ Service Gateway Registration ║" -ForegroundColor Cyan Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan Write-Host "" # Step 1: Add Service Instance Write-Host "[1/3] Adding service instance..." -ForegroundColor Yellow $instanceBody = @{ destinationId = $DestinationId address = $Address weight = $Weight } | ConvertTo-Json try { $null = Invoke-RestMethod -Uri "$GatewayUrl/api/gateway/clusters/$ClusterId/instances" ` -Method Post ` -ContentType "application/json" ` -Body $instanceBody Write-Host " ✓ Instance: $DestinationId -> $Address" -ForegroundColor Green } catch { if ($_.Exception.Response.StatusCode -eq "BadRequest") { Write-Host " ℹ Instance may already exist" -ForegroundColor Gray } else { throw $_ } } # Step 2: Add Route Write-Host "" Write-Host "[2/3] Configuring gateway route..." -ForegroundColor Yellow if ($Global) { $routeBody = @{ serviceName = $Prefix clusterId = $ClusterId pathPattern = $PathPattern } | ConvertTo-Json $routeType = "Global" } else { $routeBody = @{ serviceName = $Prefix pathPattern = $PathPattern } | ConvertTo-Json $routeType = "Tenant [$TenantCode]" } try { $routeUrl = if ($Global) { "$GatewayUrl/api/gateway/routes/global" } else { "$GatewayUrl/api/gateway/tenants/$TenantCode/routes" } $null = Invoke-RestMethod -Uri $routeUrl ` -Method Post ` -ContentType "application/json" ` -Body $routeBody Write-Host " ✓ Route: $PathPattern -> $ClusterId" -ForegroundColor Green } catch { if ($_.Exception.Response.StatusCode -eq "BadRequest") { Write-Host " ℹ Route may already exist" -ForegroundColor Gray } else { throw $_ } } # Step 3: Reload Config Write-Host "" Write-Host "[3/3] Reloading gateway configuration..." -ForegroundColor Yellow $null = Invoke-RestMethod -Uri "$GatewayUrl/api/gateway/reload" -Method Post Write-Host " ✓ Configuration reloaded" -ForegroundColor Green # Summary Write-Host "" Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Cyan Write-Host " Service Registered Successfully!" -ForegroundColor Cyan Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Cyan Write-Host "" Write-Host " Service: $Prefix" -ForegroundColor White Write-Host " Version: $Version" -ForegroundColor White Write-Host " Cluster: $ClusterId" -ForegroundColor White Write-Host " Type: $routeType" -ForegroundColor White Write-Host " Pattern: $PathPattern" -ForegroundColor White Write-Host " Address: $Address" -ForegroundColor White Write-Host "" Write-Host " Available Endpoints:" -ForegroundColor Yellow Write-Host " GET $GatewayUrl/$Prefix/$Version/campaigns" -ForegroundColor Cyan Write-Host " POST $GatewayUrl/$Prefix/$Version/campaigns" -ForegroundColor Cyan Write-Host " GET $GatewayUrl/$Prefix/$Version/campaigns/{id}" -ForegroundColor Cyan Write-Host " POST $GatewayUrl/$Prefix/$Version/campaigns/{id}/publish" -ForegroundColor Cyan Write-Host ""