< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
18%
Covered lines: 7
Uncovered lines: 30
Coverable lines: 37
Total lines: 106
Line coverage: 18.9%
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

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 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#>
 21function 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
 138        $Server = Resolve-KestrunServer -Server $Server
 139        if ($null -eq $Server) {
 040            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 41        }
 42    }
 43    process {
 44        # Start the Kestrel server
 145        if ( -not $Quiet.IsPresent ) {
 046            Write-Host "Starting Kestrun server '$($Server.ApplicationName)' ..."
 47        }
 248        $Server.StartAsync() | Out-Null
 149        if (-not $Quiet.IsPresent) {
 050            Write-Host 'Kestrun server started successfully.'
 051            foreach ($listener in $Server.Options.Listeners) {
 052                if ($listener.X509Certificate) {
 053                    Write-Host "Listening on https://$($listener.IPAddress):$($listener.Port) with protocols: $($listene
 54                } else {
 055                    Write-Host "Listening on http://$($listener.IPAddress):$($listener.Port) with protocols: $($listener
 56                }
 057                if ($listener.X509Certificate) {
 058                    Write-Host "Using certificate: $($listener.X509Certificate.Subject)"
 59                } else {
 060                    Write-Host 'No certificate configured. Running in HTTP mode.'
 61                }
 062                if ($listener.UseConnectionLogging) {
 063                    Write-Host 'Connection logging is enabled.'
 64                } else {
 065                    Write-Host 'Connection logging is disabled.'
 66                }
 67            }
 068            Write-Host 'Press Ctrl+C to stop the server.'
 69        }
 170        if (-not $NoWait.IsPresent) {
 71            # Intercept Ctrl+C and gracefully stop the Kestrun server
 72            try {
 073                [Console]::TreatControlCAsInput = $true
 074                while ($true) {
 075                    if ([Console]::KeyAvailable) {
 076                        $key = [Console]::ReadKey($true)
 077                        if (($key.Modifiers -eq 'Control') -and ($key.Key -eq 'C')) {
 078                            if (-not $Quiet.IsPresent) {
 079                                Write-Host 'Ctrl+C detected. Stopping Kestrun server...'
 80                            }
 081                            $Server.StopAsync().Wait()
 82                            break
 83                        }
 84                    }
 085                    Start-Sleep -Milliseconds 100
 86                }
 87            } finally {
 88                # Ensure the server is stopped on exit
 089                if (-not $Quiet.IsPresent) {
 090                    Write-Host 'Stopping Kestrun server...'
 91                }
 092                [Kestrun.KestrunHostManager]::StopAsync($Server.ApplicationName).Wait()
 93                #$Server.StopAsync().Wait()
 094                [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 95                #$Server.Dispose()
 096                if (-not $Quiet.IsPresent) {
 097                    Write-Host 'Kestrun server stopped.'
 98                }
 99            }
 1100        } elseif ($PassThru.IsPresent) {
 101            # if the PassThru switch is specified, return the server instance
 0102            return $Server
 103        }
 104    }
 105}
 106

Methods/Properties

Start-KrServer()