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