update.ps1 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ============================================================================
  2. # DPOT Postman Collection 갱신 스크립트
  3. #
  4. # 사용법:
  5. # 1. 백엔드 실행 (다른 터미널)
  6. # cd D:\dpot\Backend
  7. # dotnet run --project Web.Api
  8. #
  9. # 2. 이 스크립트 실행
  10. # cd D:\dpot\Backend\Web.Api\postman
  11. # pwsh ./update.ps1
  12. #
  13. # 옵션:
  14. # -ApiBase <url> 기본값: https://localhost:4000
  15. # -SkipPublic public swagger 변환 생략
  16. # -SkipInternal internal swagger 변환 생략
  17. #
  18. # 결과:
  19. # collections/DPOT-Public-API.postman_collection.json (외부 /v1/* + /oauth/token)
  20. # collections/DPOT-Internal-API.postman_collection.json (내부 /api/*)
  21. #
  22. # 요구사항:
  23. # - Node.js (npx 포함) — openapi-to-postmanv2 를 npx 로 1회성 실행
  24. # - 실행 중인 로컬 백엔드 (internal swagger 는 Production 에 차단되어 로컬 필수)
  25. # ============================================================================
  26. [CmdletBinding()]
  27. param(
  28. [string]$ApiBase = "https://localhost:4000",
  29. [switch]$SkipPublic,
  30. [switch]$SkipInternal
  31. )
  32. $ErrorActionPreference = "Stop"
  33. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  34. Set-Location $scriptDir
  35. function Write-Step($msg) { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor Cyan }
  36. function Write-Ok($msg) { Write-Host " [OK] $msg" -ForegroundColor Green }
  37. function Write-Fail($msg) { Write-Host " [!!] $msg" -ForegroundColor Red }
  38. # 1) 백엔드 health check
  39. Write-Step "백엔드 health check ($ApiBase/health)"
  40. try {
  41. $r = Invoke-WebRequest -Uri "$ApiBase/health" -SkipCertificateCheck -TimeoutSec 5 -UseBasicParsing
  42. if ($r.StatusCode -ne 200) {
  43. Write-Fail "health 응답 $($r.StatusCode) — 백엔드 실행 상태 확인 필요"
  44. exit 1
  45. }
  46. Write-Ok "백엔드 정상 응답"
  47. } catch {
  48. Write-Fail "백엔드 접근 실패: $($_.Exception.Message)"
  49. Write-Host " 먼저 다른 터미널에서 다음 명령을 실행하세요:" -ForegroundColor Yellow
  50. Write-Host " cd D:\dpot\Backend; dotnet run --project Web.Api" -ForegroundColor Yellow
  51. exit 1
  52. }
  53. # 2) Node/npx 사용 가능 확인
  54. Write-Step "Node.js / npx 확인"
  55. $nodeVer = (& node -v) 2>$null
  56. $npxVer = (& npx --version) 2>$null
  57. if (-not $nodeVer -or -not $npxVer) {
  58. Write-Fail "Node.js 또는 npx 가 없습니다. https://nodejs.org 에서 설치하세요."
  59. exit 1
  60. }
  61. Write-Ok "node $nodeVer / npx $npxVer"
  62. # 3) collections 폴더 보장
  63. $colDir = Join-Path $scriptDir "collections"
  64. if (-not (Test-Path $colDir)) {
  65. New-Item -ItemType Directory -Path $colDir | Out-Null
  66. }
  67. function Convert-Swagger {
  68. param(
  69. [string]$DocName, # public | internal
  70. [string]$OutName # DPOT-Public-API | DPOT-Internal-API
  71. )
  72. Write-Step "$DocName swagger 변환"
  73. $swaggerUrl = "$ApiBase/swagger/$DocName/swagger.json"
  74. $tmpFile = Join-Path $env:TEMP "dpot-$DocName.json"
  75. $outFile = Join-Path $colDir "$OutName.postman_collection.json"
  76. # fetch
  77. try {
  78. Invoke-WebRequest -Uri $swaggerUrl -SkipCertificateCheck -OutFile $tmpFile -UseBasicParsing | Out-Null
  79. $size = (Get-Item $tmpFile).Length
  80. Write-Ok "$swaggerUrl => $size bytes"
  81. } catch {
  82. Write-Fail "swagger fetch 실패: $($_.Exception.Message)"
  83. return
  84. }
  85. # convert
  86. # openapi-to-postmanv2 5.x 옵션:
  87. # folderStrategy=Tags tag 별 폴더 분류
  88. # enableOptionalParameters optional query/header 포함
  89. # includeAuthInfoInExample Bearer auth header 자동
  90. $opts = "folderStrategy=Tags,enableOptionalParameters=true,includeAuthInfoInExample=true"
  91. & npx -y openapi-to-postmanv2@5 -s $tmpFile -o $outFile -p -O $opts 2>&1 | Out-Host
  92. if (Test-Path $outFile) {
  93. $outSize = (Get-Item $outFile).Length
  94. $col = Get-Content $outFile -Raw | ConvertFrom-Json
  95. $folders = $col.item.Count
  96. function CountReq($items) {
  97. $n = 0
  98. foreach ($it in $items) {
  99. if ($it.request) { $n++ }
  100. elseif ($it.item) { $n += CountReq $it.item }
  101. }
  102. return $n
  103. }
  104. $reqs = CountReq $col.item
  105. Write-Ok "$OutName.postman_collection.json — $folders folders / $reqs requests / $outSize bytes"
  106. } else {
  107. Write-Fail "$OutName 변환 결과 파일이 생성되지 않음"
  108. }
  109. }
  110. if (-not $SkipPublic) { Convert-Swagger -DocName "public" -OutName "DPOT-Public-API" }
  111. if (-not $SkipInternal) { Convert-Swagger -DocName "internal" -OutName "DPOT-Internal-API" }
  112. Write-Host ""
  113. Write-Step "완료 — Postman 에서 collections/*.postman_collection.json 을 import 하세요"