| | | 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 { |
| | 0 | 41 | | $effectiveQuiet = $Quiet.IsPresent |
| | 0 | 42 | | if (-not $PSBoundParameters.ContainsKey('Quiet')) { |
| | 0 | 43 | | $effectiveQuiet = [bool]$ExecutionContext.SessionState.PSVariable.GetValue('__krRunnerQuiet', $false) |
| | | 44 | | } |
| | 0 | 45 | | $managedConsole = [bool]$ExecutionContext.SessionState.PSVariable.GetValue('__krRunnerManagedConsole', $false) |
| | | 46 | | |
| | | 47 | | # Ensure the server instance is resolved |
| | 0 | 48 | | $Server = Resolve-KestrunServer -Server $Server |
| | 0 | 49 | | $hasConsole = $false |
| | 0 | 50 | | $writeConsole = $false |
| | | 51 | | try { |
| | 0 | 52 | | if (-not $managedConsole) { |
| | 0 | 53 | | $null = [Console]::KeyAvailable |
| | 0 | 54 | | $hasConsole = $true |
| | | 55 | | } |
| | 0 | 56 | | $writeConsole = -not $effectiveQuiet |
| | | 57 | | } catch { |
| | 0 | 58 | | Write-KrLog -Level Information -Message 'No console available; running in non-interactive mode.' |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | $writeStartupFailureBanner = { |
| | | 62 | | param([Exception]$Exception) |
| | | 63 | | |
| | 0 | 64 | | if (-not $effectiveQuiet) { |
| | 0 | 65 | | $errorMessage = "Failed to start Kestrun server '$($Server.ApplicationName)'." |
| | 0 | 66 | | if ($Exception -and -not [string]::IsNullOrWhiteSpace($Exception.Message)) { |
| | 0 | 67 | | $errorMessage = "$errorMessage $($Exception.Message)" |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | Write-KrLog -Level Error -Message $errorMessage |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | process { |
| | | 75 | | # Generate OpenAPI documents if not already generated |
| | 0 | 76 | | foreach ( $doc in $Server.OpenApiDocumentDescriptor.Keys ) { |
| | 0 | 77 | | if ( -not $Server.OpenApiDocumentDescriptor[$doc].HasBeenGenerated ) { |
| | 0 | 78 | | Build-KrOpenApiDocument -Server $Server -DocId $doc |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | # Start the Kestrel server |
| | 0 | 82 | | if ( -not $effectiveQuiet ) { |
| | 0 | 83 | | Write-Host "Starting Kestrun server '$($Server.ApplicationName)' ..." |
| | 0 | 84 | | Write-KrLog -Level Information -Message "Starting Kestrun server '{ApplicationName}' ..." -Values $Server.Ap |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | $startupTask = $null |
| | | 88 | | try { |
| | 0 | 89 | | $startupTask = $Server.StartAsync() |
| | | 90 | | } catch { |
| | 0 | 91 | | $originalException = $_.Exception |
| | 0 | 92 | | $displayException = if ($originalException -and $originalException.InnerException) { |
| | 0 | 93 | | $originalException.InnerException |
| | | 94 | | } else { |
| | 0 | 95 | | $originalException |
| | | 96 | | } |
| | 0 | 97 | | & $writeStartupFailureBanner $displayException |
| | | 98 | | throw |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if ($NoWait.IsPresent) { |
| | | 102 | | # Preserve the -NoWait contract: return immediately after startup is initiated. |
| | 0 | 103 | | if ($startupTask.IsFaulted) { |
| | 0 | 104 | | $startupException = if ($startupTask.Exception -and $startupTask.Exception.InnerException) { |
| | 0 | 105 | | $startupTask.Exception.InnerException |
| | | 106 | | } else { |
| | 0 | 107 | | $startupTask.Exception |
| | | 108 | | } |
| | 0 | 109 | | & $writeStartupFailureBanner $startupException |
| | 0 | 110 | | if ($startupTask.Exception -and $startupTask.Exception.InnerException) { |
| | 0 | 111 | | throw $startupTask.Exception.InnerException |
| | | 112 | | } |
| | 0 | 113 | | throw $startupTask.Exception |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if ($writeConsole) { |
| | 0 | 117 | | Write-Host 'Kestrun server startup initiated (NoWait).' |
| | | 118 | | } |
| | | 119 | | } else { |
| | | 120 | | try { |
| | | 121 | | # Block until startup completes so bind/config errors surface immediately. |
| | 0 | 122 | | $null = $startupTask.GetAwaiter().GetResult() |
| | | 123 | | } catch { |
| | 0 | 124 | | $originalException = $_.Exception |
| | 0 | 125 | | $displayException = if ($originalException -and $originalException.InnerException) { |
| | 0 | 126 | | $originalException.InnerException |
| | | 127 | | } else { |
| | 0 | 128 | | $originalException |
| | | 129 | | } |
| | 0 | 130 | | & $writeStartupFailureBanner $displayException |
| | | 131 | | throw |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if ($writeConsole) { |
| | 0 | 135 | | Write-Host 'Kestrun server started successfully.' |
| | 0 | 136 | | foreach ($listener in $Server.Options.Listeners) { |
| | 0 | 137 | | if ($listener.X509Certificate) { |
| | 0 | 138 | | Write-Host "Listening on https://$($listener.IPAddress):$($listener.Port) with protocols: $($lis |
| | | 139 | | } else { |
| | 0 | 140 | | Write-Host "Listening on http://$($listener.IPAddress):$($listener.Port) with protocols: $($list |
| | | 141 | | } |
| | 0 | 142 | | if ($listener.X509Certificate) { |
| | 0 | 143 | | Write-Host "Using certificate: $($listener.X509Certificate.Subject)" |
| | | 144 | | } else { |
| | 0 | 145 | | Write-Host 'No certificate configured. Running in HTTP mode.' |
| | | 146 | | } |
| | 0 | 147 | | if ($listener.UseConnectionLogging) { |
| | 0 | 148 | | Write-Host 'Connection logging is enabled.' |
| | | 149 | | } else { |
| | 0 | 150 | | Write-Host 'Connection logging is disabled.' |
| | | 151 | | } |
| | | 152 | | } |
| | 0 | 153 | | Write-Host 'Press Ctrl+C to stop the server.' |
| | | 154 | | } |
| | | 155 | | } |
| | 0 | 156 | | if (-not $NoWait.IsPresent) { |
| | | 157 | | # Intercept Ctrl+C and gracefully stop the Kestrun server |
| | | 158 | | try { |
| | 0 | 159 | | if ($hasConsole) { |
| | 0 | 160 | | [Console]::TreatControlCAsInput = $true |
| | 0 | 161 | | while ($Server.IsRunning) { |
| | 0 | 162 | | if ([Console]::KeyAvailable) { |
| | 0 | 163 | | $key = [Console]::ReadKey($true) |
| | 0 | 164 | | if (($key.Modifiers -eq 'Control') -and ($key.Key -eq 'C')) { |
| | 0 | 165 | | if ($writeConsole) { |
| | 0 | 166 | | Write-Host 'Ctrl+C detected. Stopping Kestrun server...' |
| | | 167 | | } |
| | 0 | 168 | | $null = $Server.StopAsync().Wait() |
| | | 169 | | break |
| | | 170 | | } |
| | | 171 | | } |
| | 0 | 172 | | Start-Sleep -Milliseconds 100 |
| | | 173 | | } |
| | | 174 | | } else { |
| | | 175 | | # Just wait for the server to stop (block until externally stopped) |
| | 0 | 176 | | while ($Server.IsRunning) { |
| | 0 | 177 | | Start-Sleep -Seconds 1 |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | } finally { |
| | | 181 | | # Ensure the server is stopped on exit |
| | 0 | 182 | | if ($writeConsole) { |
| | 0 | 183 | | Write-Host 'Stopping Kestrun server...' |
| | | 184 | | } |
| | 0 | 185 | | if ($Server.IsRunning) { |
| | 0 | 186 | | $null = [Kestrun.KestrunHostManager]::StopAsync($Server.ApplicationName).Wait() |
| | | 187 | | } |
| | | 188 | | #$Server.StopAsync().Wait() |
| | 0 | 189 | | [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName) |
| | | 190 | | |
| | 0 | 191 | | if ($CloseLogsOnExit.IsPresent) { |
| | | 192 | | # Close the Kestrel loggers |
| | 0 | 193 | | Close-KrLogger |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | if ($writeConsole) { |
| | 0 | 197 | | Write-Host 'Kestrun server stopped.' |
| | | 198 | | } |
| | | 199 | | } |
| | 0 | 200 | | } elseif ($PassThru.IsPresent) { |
| | | 201 | | # if the PassThru switch is specified, return the server instance |
| | 0 | 202 | | return $Server |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |