< 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@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 136
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 - 14:53:17 Line coverage: 17.6% (6/34) Total lines: 98 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/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@10d476bee71c71ad215bb8ab59f219887b5b4a5e12/12/2025 - 17:27:19 Line coverage: 0% (0/50) Total lines: 136 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

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        # Generate OpenAPI documents if not already generated
 055        foreach ( $doc in $Server.OpenApiDocumentDescriptor.Keys ) {
 056            if ( -not $Server.OpenApiDocumentDescriptor[$doc].HasBeenGenerated ) {
 057                Build-KrOpenApiDocument -Server $Server -DocId $doc
 58            }
 59        }
 60        # Start the Kestrel server
 061        if ( -not $Quiet.IsPresent ) {
 062            Write-Host "Starting Kestrun server '$($Server.ApplicationName)' ..."
 63        }
 064        $Server.StartAsync() | Out-Null
 065        if ($writeConsole) {
 066            Write-Host 'Kestrun server started successfully.'
 067            foreach ($listener in $Server.Options.Listeners) {
 068                if ($listener.X509Certificate) {
 069                    Write-Host "Listening on https://$($listener.IPAddress):$($listener.Port) with protocols: $($listene
 70                } else {
 071                    Write-Host "Listening on http://$($listener.IPAddress):$($listener.Port) with protocols: $($listener
 72                }
 073                if ($listener.X509Certificate) {
 074                    Write-Host "Using certificate: $($listener.X509Certificate.Subject)"
 75                } else {
 076                    Write-Host 'No certificate configured. Running in HTTP mode.'
 77                }
 078                if ($listener.UseConnectionLogging) {
 079                    Write-Host 'Connection logging is enabled.'
 80                } else {
 081                    Write-Host 'Connection logging is disabled.'
 82                }
 83            }
 084            Write-Host 'Press Ctrl+C to stop the server.'
 85        }
 086        if (-not $NoWait.IsPresent) {
 87            # Intercept Ctrl+C and gracefully stop the Kestrun server
 88            try {
 089                if ($hasConsole) {
 090                    [Console]::TreatControlCAsInput = $true
 091                    while ($Server.IsRunning) {
 092                        if ([Console]::KeyAvailable) {
 093                            $key = [Console]::ReadKey($true)
 094                            if (($key.Modifiers -eq 'Control') -and ($key.Key -eq 'C')) {
 095                                if ($writeConsole) {
 096                                    Write-Host 'Ctrl+C detected. Stopping Kestrun server...'
 97                                }
 098                                $Server.StopAsync().Wait()
 99                                break
 100                            }
 101                        }
 0102                        Start-Sleep -Milliseconds 100
 103                    }
 104                } else {
 105                    # Just wait for the server to stop (block until externally stopped)
 0106                    while ($Server.IsRunning) {
 0107                        Start-Sleep -Seconds 1
 108                    }
 109                }
 110            } finally {
 111                # Ensure the server is stopped on exit
 0112                if ($writeConsole) {
 0113                    Write-Host 'Stopping Kestrun server...'
 114                }
 0115                if ($Server.IsRunning) {
 0116                    [Kestrun.KestrunHostManager]::StopAsync($Server.ApplicationName).Wait()
 117                }
 118                #$Server.StopAsync().Wait()
 0119                [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 120
 0121                if ($CloseLogsOnExit.IsPresent) {
 122                    # Close the Kestrel loggers
 0123                    Close-KrLogger
 124                }
 125
 0126                if ($writeConsole) {
 0127                    Write-Host 'Kestrun server stopped.'
 128                }
 129            }
 0130        } elseif ($PassThru.IsPresent) {
 131            # if the PassThru switch is specified, return the server instance
 0132            return $Server
 133        }
 134    }
 135}
 136

Methods/Properties

Start-KrServer()