| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Starts the Kestrun server and listens for incoming requests. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function starts the Kestrun server, allowing it to accept incoming HTTP requests. |
| | 6 | | .PARAMETER Server |
| | 7 | | The Kestrun server instance to start. This parameter is mandatory. |
| | 8 | | .PARAMETER NoWait |
| | 9 | | If specified, the function will not wait for the server to start and will return immediately. |
| | 10 | | .PARAMETER Quiet |
| | 11 | | If specified, suppresses output messages during the startup process. |
| | 12 | | .EXAMPLE |
| | 13 | | Start-KrServer -Server $server |
| | 14 | | Starts the specified Kestrun server instance and listens for incoming requests. |
| | 15 | | .NOTES |
| | 16 | | This function is designed to be used after the server has been configured and routes have been added. |
| | 17 | | It will block the console until the server is stopped or Ctrl+C is pressed. |
| | 18 | | #> |
| | 19 | | function Stop-KrServer { |
| | 20 | | [KestrunRuntimeApi('Definition')] |
| | 21 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 22 | | [CmdletBinding()] |
| | 23 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')] |
| | 24 | | param( |
| | 25 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 26 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 27 | | [Parameter()] |
| | 28 | | [switch]$NoWait, |
| | 29 | | [Parameter()] |
| | 30 | | [switch]$Quiet |
| | 31 | | ) |
| | 32 | | begin { |
| | 33 | | # Ensure the server instance is resolved |
| 1 | 34 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 35 | | if ($null -eq $Server) { |
| 0 | 36 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 37 | | } |
| | 38 | | } |
| | 39 | | process { |
| | 40 | | # Stop the Kestrel server |
| 2 | 41 | | $Server.StopAsync() | Out-Null |
| | 42 | | # Ensure the server is stopped on exit |
| 1 | 43 | | if (-not $Quiet.IsPresent) { |
| 0 | 44 | | Write-Host 'Stopping Kestrun server...' -NoNewline |
| | 45 | | } |
| | 46 | |
|
| | 47 | | # If NoWait is specified, return immediately |
| 1 | 48 | | if ($NoWait.IsPresent) { |
| | 49 | | return |
| | 50 | | } |
| | 51 | |
|
| 1 | 52 | | while ($Server.IsRunning) { |
| 0 | 53 | | Start-Sleep -Seconds 1 |
| 0 | 54 | | if (-not $Quiet.IsPresent) { |
| 0 | 55 | | Write-Host '#' -NoNewline |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| 1 | 59 | | [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName) |
| | 60 | |
|
| 1 | 61 | | if (-not $Quiet.IsPresent) { |
| 0 | 62 | | Write-Host 'Kestrun server stopped.' |
| | 63 | | } |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|