-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dev.ps1
More file actions
250 lines (215 loc) · 9.43 KB
/
start-dev.ps1
File metadata and controls
250 lines (215 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# MarkItDown API - Development Start Script
# Builds, validates environment, and starts the API
param(
[switch]$NoBuild,
[switch]$Release,
[switch]$NoBrowser,
[int]$Port = 5001
)
$ErrorActionPreference = "Stop"
$scriptDir = $PSScriptRoot
$apiProjectDir = Join-Path $scriptDir "src\MarkItDownAPI.Api"
$httpPort = $Port - 1
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host " MarkItDown API - Development Server" -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
# ============================================
# STEP 1: Load and validate configuration
# ============================================
Write-Host "`n[1/6] Checking configuration..." -ForegroundColor Yellow
$appSettingsPath = Join-Path $apiProjectDir "appsettings.json"
if (-not (Test-Path $appSettingsPath)) {
Write-Host "ERROR: appsettings.json not found!" -ForegroundColor Red
Write-Host "Expected at: $appSettingsPath" -ForegroundColor Yellow
exit 1
}
$settings = Get-Content $appSettingsPath -Raw | ConvertFrom-Json
$pythonHome = $settings.Python.PythonHome
$pythonDll = $settings.Python.PythonDll
if ([string]::IsNullOrEmpty($pythonHome)) {
# Try to auto-detect Python
$possiblePaths = @(
"C:\Python312",
"C:\Python311",
"C:\Python310",
"$env:LOCALAPPDATA\Programs\Python\Python312",
"$env:LOCALAPPDATA\Programs\Python\Python311",
"$env:LOCALAPPDATA\Programs\Python\Python310"
)
foreach ($path in $possiblePaths) {
if (Test-Path "$path\python.exe") {
$pythonHome = $path
Write-Host " Auto-detected Python at: $pythonHome" -ForegroundColor Gray
break
}
}
}
Write-Host "Configuration loaded" -ForegroundColor Green
# ============================================
# STEP 2: Validate Python installation
# ============================================
Write-Host "`n[2/6] Validating Python environment..." -ForegroundColor Yellow
if ([string]::IsNullOrEmpty($pythonHome)) {
Write-Host "ERROR: Python not found. Please set Python:PythonHome in appsettings.json" -ForegroundColor Red
exit 1
}
$pythonExe = Join-Path $pythonHome "python.exe"
if (-not (Test-Path $pythonExe)) {
Write-Host "ERROR: Python not found at: $pythonHome" -ForegroundColor Red
exit 1
}
# Check Python version
$pythonVersion = & $pythonExe --version 2>&1
Write-Host " Python: $pythonVersion" -ForegroundColor Gray
# Check Python DLL
$dllPath = Join-Path $pythonHome $pythonDll
if (-not (Test-Path $dllPath)) {
Write-Host "WARNING: Python DLL not found at: $dllPath" -ForegroundColor Yellow
$foundDll = Get-ChildItem "$pythonHome\python*.dll" -ErrorAction SilentlyContinue |
Where-Object { $_.Name -match "^python\d{2,3}\.dll$" } |
Select-Object -First 1
if ($foundDll) {
$dllPath = $foundDll.FullName
Write-Host " Found: $($foundDll.Name)" -ForegroundColor Green
} else {
Write-Host "ERROR: No Python DLL found in $pythonHome" -ForegroundColor Red
exit 1
}
}
# Check MarkItDown installation
Write-Host " Checking MarkItDown..." -ForegroundColor Gray
$ErrorActionPreference = 'SilentlyContinue'
$markitdownCheck = & $pythonExe -c "import markitdown; print(markitdown.__version__)" 2>$null
$markitdownExitCode = $LASTEXITCODE
$ErrorActionPreference = 'Stop'
if ($markitdownExitCode -ne 0 -or -not $markitdownCheck) {
Write-Host "ERROR: MarkItDown not installed in Python environment" -ForegroundColor Red
Write-Host "Run: $pythonExe -m pip install 'markitdown[all]'" -ForegroundColor Yellow
exit 1
}
Write-Host " MarkItDown: v$markitdownCheck" -ForegroundColor Gray
Write-Host "Python environment OK" -ForegroundColor Green
# ============================================
# STEP 3: Kill existing API processes
# ============================================
Write-Host "`n[3/6] Stopping existing API processes..." -ForegroundColor Yellow
# Kill any running MarkItDownAPI.Api processes
$apiProcesses = Get-Process -Name "MarkItDownAPI.Api" -ErrorAction SilentlyContinue
if ($apiProcesses) {
foreach ($proc in $apiProcesses) {
Write-Host " Stopping MarkItDownAPI.Api (PID: $($proc.Id))" -ForegroundColor Yellow
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 1
Write-Host "Existing processes stopped" -ForegroundColor Green
} else {
Write-Host "No existing API processes found" -ForegroundColor Green
}
# Kill processes occupying our ports
$portsToCheck = @($Port, $httpPort)
foreach ($checkPort in $portsToCheck) {
$netstatOutput = netstat -ano | Select-String ":$checkPort\s" | Select-String "LISTENING"
if ($netstatOutput) {
foreach ($line in $netstatOutput) {
$parts = $line.ToString().Trim() -split '\s+'
$pid = $parts[-1]
if ($pid -and $pid -match '^\d+$' -and [int]$pid -gt 0) {
try {
$process = Get-Process -Id $pid -ErrorAction SilentlyContinue
if ($process) {
Write-Host " Killing process $($process.ProcessName) (PID: $pid) on port $checkPort" -ForegroundColor Yellow
Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue
}
}
catch {
# Process may have already exited
}
}
}
}
}
# ============================================
# STEP 4: Build the project
# ============================================
if (-not $NoBuild) {
Write-Host "`n[4/6] Building project..." -ForegroundColor Yellow
$buildConfig = if ($Release) { "Release" } else { "Debug" }
Push-Location $scriptDir
try {
dotnet build MarkItDownAPI.slnx -c $buildConfig --nologo
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Build failed!" -ForegroundColor Red
exit 1
}
Write-Host "Build successful ($buildConfig)" -ForegroundColor Green
}
finally {
Pop-Location
}
} else {
Write-Host "`n[4/6] Skipping build (-NoBuild specified)" -ForegroundColor Gray
}
# ============================================
# STEP 5: Verify ports are available
# ============================================
Write-Host "`n[5/6] Verifying ports are available..." -ForegroundColor Yellow
$portsBlocked = $false
foreach ($checkPort in $portsToCheck) {
$netstatOutput = netstat -ano | Select-String ":$checkPort\s" | Select-String "LISTENING"
if ($netstatOutput) {
Write-Host " WARNING: Port $checkPort is still in use" -ForegroundColor Yellow
$portsBlocked = $true
}
}
if ($portsBlocked) {
Write-Host "Some ports may be blocked. API might fail to start." -ForegroundColor Yellow
} else {
Write-Host "All ports available" -ForegroundColor Green
}
# ============================================
# STEP 6: Start the API in a new console window
# ============================================
Write-Host "`n[6/6] Starting API server in new window..." -ForegroundColor Yellow
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host " Server will start on:" -ForegroundColor White
Write-Host " https://localhost:$Port" -ForegroundColor Green
Write-Host " http://localhost:$httpPort" -ForegroundColor Green
Write-Host "`n Documentation:" -ForegroundColor White
Write-Host " https://localhost:$Port/scalar/v1" -ForegroundColor Green
Write-Host "`n API Endpoints:" -ForegroundColor White
Write-Host " POST /api/convert/file - Upload file" -ForegroundColor Gray
Write-Host " POST /api/convert/url - Convert URL" -ForegroundColor Gray
Write-Host " GET /api/convert/supported-formats - Supported formats" -ForegroundColor Gray
Write-Host " GET /api/convert/health - Health check" -ForegroundColor Gray
Write-Host "=========================================" -ForegroundColor Cyan
# Build the command to run in the new window
$runConfig = if ($Release) { "Release" } else { "Debug" }
$dotnetCommand = "dotnet run -c $runConfig --no-build --urls `"https://localhost:$Port;http://localhost:$httpPort`""
# Create a script block that sets up the environment and runs the API
$startupScript = @"
`$Host.UI.RawUI.WindowTitle = 'MarkItDown API - localhost:$Port'
`$env:PYTHONHOME = '$pythonHome'
`$env:PYTHONNET_PYDLL = '$dllPath'
`$env:PATH = '$pythonHome;$pythonHome\Scripts;' + `$env:PATH
Set-Location '$apiProjectDir'
Write-Host 'MarkItDown API Server' -ForegroundColor Cyan
Write-Host '=====================' -ForegroundColor Cyan
Write-Host 'Press Ctrl+C to stop the server' -ForegroundColor Yellow
Write-Host ''
$dotnetCommand
Write-Host ''
Write-Host 'Server stopped. Press any key to close...' -ForegroundColor Yellow
`$null = `$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
"@
# Start the API in a new PowerShell window
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($startupScript))
Start-Process powershell -ArgumentList "-NoExit", "-EncodedCommand", $encodedCommand
Write-Host "`nAPI server started in new console window" -ForegroundColor Green
# Open Scalar documentation in browser after a short delay
if (-not $NoBrowser) {
$scalarUrl = "https://localhost:$Port/scalar/v1"
Write-Host "Opening Scalar documentation in browser..." -ForegroundColor Gray
Start-Sleep -Seconds 3
Start-Process $scalarUrl
}
Write-Host "`nDone! You can close this window." -ForegroundColor Green