Full Server Demo
Run an integrated demo server with multiple endpoints, basic routes, and clean start/stop.
Full source
File: pwsh/tutorial/examples/14.2-Full-Demo.ps1
<#
14.1 Full Demo (moved from 7.9)
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
New-KrLogger | Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault | Out-Null
$certPath = Join-Path $PSScriptRoot 'fulldemo.pfx'
if (-not (Test-Path $certPath)) {
$demoPwd = Read-Host -Prompt 'Enter temporary password for fulldemo.pfx (dev only)' -AsSecureString
New-KrSelfSignedCertificate -DnsName 'localhost' -Path $certPath -Password $demoPwd | Out-Null
} else {
$demoPwd = Read-Host -Prompt 'Enter existing fulldemo.pfx password' -AsSecureString
}
$srv = New-KrServer -Name 'Full Demo Server' -PassThru
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
Add-KrEndpoint -Port ($Port + 433) -IPAddress $IPAddress -CertPath $certPath -CertPassword $demoPwd
if ($IsWindows) { Add-KrNamedPipeListener -PipeName 'kestrun.full.pipe' }
Enable-KrConfiguration
Set-KrServerLimit -MaxRequestBodySize 2097152 -MaxConcurrentConnections 100 | Out-Null
Set-KrServerOptions -DenyServerHeader -MaxRunspaces 8 -MinRunspaces 2 | Out-Null
Add-KrMapRoute -Verbs Get -Pattern '/health' -ScriptBlock { Write-KrJsonResponse @{ ok = $true } }
Add-KrMapRoute -Verbs Get -Pattern '/version' -ScriptBlock { Write-KrJsonResponse @{ version = '1.0'; time = (Get-Date) } }
Start-KrServer -Server $srv -NoWait | Out-Null
Write-Host 'Demo running. Press Enter to stop...'
[void][Console]::ReadLine()
Stop-KrServer -Server $srv
Remove-KrServer -Name $srv.ApplicationName
Step-by-step
- Logging: Register a console logger.
- Certificates: Create (or reuse) a dev certificate for HTTPS.
- Server: Create a named server and add HTTP/HTTPS endpoints.
- Routes: Map a small set of demo routes (for example
/healthand/version). - Lifecycle: Start non-blocking, wait for input, then stop and remove the server.
Try it
# Run the demo (defaults to http://127.0.0.1:5000)
pwsh .\docs\_includes\examples\pwsh\14.2-Full-Demo.ps1
# In another terminal
curl -i http://127.0.0.1:5000/health
curl -i http://127.0.0.1:5000/version
References
- New-KrServer
- Add-KrEndpoint
- Enable-KrConfiguration
- Start-KrServer
- Stop-KrServer
- Remove-KrServer
- New-KrSelfSignedCertificate
- Add-KrMapRoute
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| HTTPS endpoint fails to start | Missing/incorrect PFX password | Re-run the script and enter the correct password when prompted |
| Port already in use | Another process is listening on $Port | Stop the other process or run the script with -Port set to a free port |
| Script waits for input | Demo uses a “press Enter to stop” pattern | Press Enter to stop, or remove the console read line if running non-interactively |
Previous / Next
Previous: Start/Stop Patterns Next: Middleware