| | | 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 | | .PARAMETER CloseLogsOnExit |
| | | 13 | | If specified, closes all loggers when the server stops. |
| | | 14 | | .PARAMETER PassThru |
| | | 15 | | If specified, the cmdlet will return the modified server instance after starting it. |
| | | 16 | | .EXAMPLE |
| | | 17 | | Start-KrServer -Server $server |
| | | 18 | | Starts the specified Kestrun server instance and listens for incoming requests. |
| | | 19 | | .NOTES |
| | | 20 | | This function is designed to be used after the server has been configured and routes have been added. |
| | | 21 | | It will block the console until the server is stopped or Ctrl+C is pressed. |
| | | 22 | | #> |
| | | 23 | | function Start-KrServer { |
| | | 24 | | [KestrunRuntimeApi('Definition')] |
| | | 25 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 26 | | [CmdletBinding()] |
| | | 27 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')] |
| | | 28 | | param( |
| | | 29 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 30 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 31 | | [Parameter()] |
| | | 32 | | [switch]$NoWait, |
| | | 33 | | [Parameter()] |
| | | 34 | | [switch]$Quiet, |
| | | 35 | | [Parameter()] |
| | | 36 | | [switch]$PassThru, |
| | | 37 | | [Parameter()] |
| | | 38 | | [switch]$CloseLogsOnExit |
| | | 39 | | ) |
| | | 40 | | begin { |
| | | 41 | | # Ensure the server instance is resolved |
| | 0 | 42 | | $Server = Resolve-KestrunServer -Server $Server |
| | 0 | 43 | | $hasConsole = $false |
| | 0 | 44 | | $writeConsole = $false |
| | | 45 | | try { |
| | 0 | 46 | | $null = [Console]::KeyAvailable |
| | 0 | 47 | | $hasConsole = $true |
| | 0 | 48 | | $writeConsole = -not $Quiet.IsPresent |
| | | 49 | | } catch { |
| | 0 | 50 | | Write-KrLog -Level Information -Message "No console available; running in non-interactive mode." |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | process { |
| | | 54 | | # Start the Kestrel server |
| | 0 | 55 | | if ( -not $Quiet.IsPresent ) { |
| | 0 | 56 | | Write-Host "Starting Kestrun server '$($Server.ApplicationName)' ..." |
| | | 57 | | } |
| | 0 | 58 | | $Server.StartAsync() | Out-Null |
| | 0 | 59 | | if ($writeConsole) { |
| | 0 | 60 | | Write-Host 'Kestrun server started successfully.' |
| | 0 | 61 | | foreach ($listener in $Server.Options.Listeners) { |
| | 0 | 62 | | if ($listener.X509Certificate) { |
| | 0 | 63 | | Write-Host "Listening on https://$($listener.IPAddress):$($listener.Port) with protocols: $($listene |
| | | 64 | | } else { |
| | 0 | 65 | | Write-Host "Listening on http://$($listener.IPAddress):$($listener.Port) with protocols: $($listener |
| | | 66 | | } |
| | 0 | 67 | | if ($listener.X509Certificate) { |
| | 0 | 68 | | Write-Host "Using certificate: $($listener.X509Certificate.Subject)" |
| | | 69 | | } else { |
| | 0 | 70 | | Write-Host 'No certificate configured. Running in HTTP mode.' |
| | | 71 | | } |
| | 0 | 72 | | if ($listener.UseConnectionLogging) { |
| | 0 | 73 | | Write-Host 'Connection logging is enabled.' |
| | | 74 | | } else { |
| | 0 | 75 | | Write-Host 'Connection logging is disabled.' |
| | | 76 | | } |
| | | 77 | | } |
| | 0 | 78 | | Write-Host 'Press Ctrl+C to stop the server.' |
| | | 79 | | } |
| | 0 | 80 | | if (-not $NoWait.IsPresent) { |
| | | 81 | | # Intercept Ctrl+C and gracefully stop the Kestrun server |
| | | 82 | | try { |
| | 0 | 83 | | if ($hasConsole) { |
| | 0 | 84 | | [Console]::TreatControlCAsInput = $true |
| | 0 | 85 | | while ($Server.IsRunning) { |
| | 0 | 86 | | if ([Console]::KeyAvailable) { |
| | 0 | 87 | | $key = [Console]::ReadKey($true) |
| | 0 | 88 | | if (($key.Modifiers -eq 'Control') -and ($key.Key -eq 'C')) { |
| | 0 | 89 | | if ($writeConsole) { |
| | 0 | 90 | | Write-Host 'Ctrl+C detected. Stopping Kestrun server...' |
| | | 91 | | } |
| | 0 | 92 | | $Server.StopAsync().Wait() |
| | | 93 | | break |
| | | 94 | | } |
| | | 95 | | } |
| | 0 | 96 | | Start-Sleep -Milliseconds 100 |
| | | 97 | | } |
| | | 98 | | } else { |
| | | 99 | | # Just wait for the server to stop (block until externally stopped) |
| | 0 | 100 | | while ($Server.IsRunning) { |
| | 0 | 101 | | Start-Sleep -Seconds 1 |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | } finally { |
| | | 105 | | # Ensure the server is stopped on exit |
| | 0 | 106 | | if ($writeConsole) { |
| | 0 | 107 | | Write-Host 'Stopping Kestrun server...' |
| | | 108 | | } |
| | 0 | 109 | | if ($Server.IsRunning) { |
| | 0 | 110 | | [Kestrun.KestrunHostManager]::StopAsync($Server.ApplicationName).Wait() |
| | | 111 | | } |
| | | 112 | | #$Server.StopAsync().Wait() |
| | 0 | 113 | | [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName) |
| | | 114 | | |
| | 0 | 115 | | if ($CloseLogsOnExit.IsPresent) { |
| | | 116 | | # Close the Kestrel loggers |
| | 0 | 117 | | Close-KrLogger |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | if ($writeConsole) { |
| | 0 | 121 | | Write-Host 'Kestrun server stopped.' |
| | | 122 | | } |
| | | 123 | | } |
| | 0 | 124 | | } elseif ($PassThru.IsPresent) { |
| | | 125 | | # if the PassThru switch is specified, return the server instance |
| | 0 | 126 | | return $Server |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |