| | | 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('Everywhere')] |
| | | 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 |
| | 0 | 34 | | $Server = Resolve-KestrunServer -Server $Server |
| | 0 | 35 | | $writeConsole = $false |
| | | 36 | | try { |
| | 0 | 37 | | $null = [Console]::KeyAvailable |
| | 0 | 38 | | $writeConsole = -not $Quiet.IsPresent |
| | | 39 | | } catch { |
| | 0 | 40 | | Write-KrLog -Level Information -Message 'No console available; running in non-interactive mode.' |
| | 0 | 41 | | $writeConsole = $false |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | process { |
| | 0 | 45 | | if ($null -ne $Context -and $null -ne $Context.Response) { |
| | | 46 | | # If called within a route, send a response before stopping |
| | 0 | 47 | | Write-KrTextResponse -InputObject 'Server is stopping...' -StatusCode 202 |
| | 0 | 48 | | Start-Sleep -Seconds 1 |
| | 0 | 49 | | $Server.StopAsync() | Out-Null |
| | | 50 | | return |
| | | 51 | | } |
| | | 52 | | # Stop the Kestrel server |
| | 0 | 53 | | $Server.StopAsync() | Out-Null |
| | | 54 | | # Ensure the server is stopped on exit |
| | 0 | 55 | | if ($writeConsole) { |
| | 0 | 56 | | Write-Host 'Stopping Kestrun server...' -NoNewline |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | # If NoWait is specified, return immediately |
| | 0 | 60 | | if ($NoWait.IsPresent) { |
| | | 61 | | return |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | while ($Server.IsRunning) { |
| | 0 | 65 | | Start-Sleep -Seconds 1 |
| | 0 | 66 | | if ($writeConsole) { |
| | 0 | 67 | | Write-Host '#' -NoNewline |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName) |
| | | 72 | | |
| | 0 | 73 | | if ($writeConsole) { |
| | 0 | 74 | | Write-Host 'Kestrun server stopped.' |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |