< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Start-KrServer
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Start-KrServer.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 130
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 17.6% (6/34) Total lines: 98 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/03/2025 - 13:55:51 Line coverage: 17.1% (6/35) Total lines: 100 Tag: Kestrun/Kestrun@80ce2a54be2f719c7be1c21a92a8156bfdc48eb409/04/2025 - 18:11:31 Line coverage: 17.1% (6/35) Total lines: 101 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/08/2025 - 20:34:03 Line coverage: 18.9% (7/37) Total lines: 106 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7209/15/2025 - 19:16:35 Line coverage: 17.9% (7/39) Total lines: 115 Tag: Kestrun/Kestrun@bfb58693b9baaed61644ace5b29e014d9ffacbc910/13/2025 - 16:52:37 Line coverage: 0% (0/47) Total lines: 130 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Start-KrServer.ps1

#LineLine coverage
 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#>
 23function 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
 042        $Server = Resolve-KestrunServer -Server $Server
 043        $hasConsole = $false
 044        $writeConsole = $false
 45        try {
 046            $null = [Console]::KeyAvailable
 047            $hasConsole = $true
 048            $writeConsole = -not $Quiet.IsPresent
 49        } catch {
 050            Write-KrLog -Level Information -Message "No console available; running in non-interactive mode."
 51        }
 52    }
 53    process {
 54        # Start the Kestrel server
 055        if ( -not $Quiet.IsPresent ) {
 056            Write-Host "Starting Kestrun server '$($Server.ApplicationName)' ..."
 57        }
 058        $Server.StartAsync() | Out-Null
 059        if ($writeConsole) {
 060            Write-Host 'Kestrun server started successfully.'
 061            foreach ($listener in $Server.Options.Listeners) {
 062                if ($listener.X509Certificate) {
 063                    Write-Host "Listening on https://$($listener.IPAddress):$($listener.Port) with protocols: $($listene
 64                } else {
 065                    Write-Host "Listening on http://$($listener.IPAddress):$($listener.Port) with protocols: $($listener
 66                }
 067                if ($listener.X509Certificate) {
 068                    Write-Host "Using certificate: $($listener.X509Certificate.Subject)"
 69                } else {
 070                    Write-Host 'No certificate configured. Running in HTTP mode.'
 71                }
 072                if ($listener.UseConnectionLogging) {
 073                    Write-Host 'Connection logging is enabled.'
 74                } else {
 075                    Write-Host 'Connection logging is disabled.'
 76                }
 77            }
 078            Write-Host 'Press Ctrl+C to stop the server.'
 79        }
 080        if (-not $NoWait.IsPresent) {
 81            # Intercept Ctrl+C and gracefully stop the Kestrun server
 82            try {
 083                if ($hasConsole) {
 084                    [Console]::TreatControlCAsInput = $true
 085                    while ($Server.IsRunning) {
 086                        if ([Console]::KeyAvailable) {
 087                            $key = [Console]::ReadKey($true)
 088                            if (($key.Modifiers -eq 'Control') -and ($key.Key -eq 'C')) {
 089                                if ($writeConsole) {
 090                                    Write-Host 'Ctrl+C detected. Stopping Kestrun server...'
 91                                }
 092                                $Server.StopAsync().Wait()
 93                                break
 94                            }
 95                        }
 096                        Start-Sleep -Milliseconds 100
 97                    }
 98                } else {
 99                    # Just wait for the server to stop (block until externally stopped)
 0100                    while ($Server.IsRunning) {
 0101                        Start-Sleep -Seconds 1
 102                    }
 103                }
 104            } finally {
 105                # Ensure the server is stopped on exit
 0106                if ($writeConsole) {
 0107                    Write-Host 'Stopping Kestrun server...'
 108                }
 0109                if ($Server.IsRunning) {
 0110                    [Kestrun.KestrunHostManager]::StopAsync($Server.ApplicationName).Wait()
 111                }
 112                #$Server.StopAsync().Wait()
 0113                [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 114
 0115                if ($CloseLogsOnExit.IsPresent) {
 116                    # Close the Kestrel loggers
 0117                    Close-KrLogger
 118                }
 119
 0120                if ($writeConsole) {
 0121                    Write-Host 'Kestrun server stopped.'
 122                }
 123            }
 0124        } elseif ($PassThru.IsPresent) {
 125            # if the PassThru switch is specified, return the server instance
 0126            return $Server
 127        }
 128    }
 129}
 130

Methods/Properties

Start-KrServer()