Public/Server/Stop-KrServer.ps1

<#
    .SYNOPSIS
        Starts the Kestrun server and listens for incoming requests.
    .DESCRIPTION
        This function starts the Kestrun server, allowing it to accept incoming HTTP requests.
    .PARAMETER Server
        The Kestrun server instance to start. This parameter is mandatory.
    .PARAMETER NoWait
        If specified, the function will not wait for the server to start and will return immediately.
    .PARAMETER Quiet
        If specified, suppresses output messages during the startup process.
    .EXAMPLE
        Start-KrServer -Server $server
        Starts the specified Kestrun server instance and listens for incoming requests.
    .NOTES
        This function is designed to be used after the server has been configured and routes have been added.
        It will block the console until the server is stopped or Ctrl+C is pressed.
#>

function Stop-KrServer {
    [KestrunRuntimeApi('Definition')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
    param(
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [Kestrun.Hosting.KestrunHost]$Server,
        [Parameter()]
        [switch]$NoWait,
        [Parameter()]
        [switch]$Quiet
    )
    begin {
        # Ensure the server instance is resolved
        $Server = Resolve-KestrunServer -Server $Server
        if ($null -eq $Server) {
            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
        }
    }
    process {
        # Stop the Kestrel server
        $Server.StopAsync() | Out-Null
        # Ensure the server is stopped on exit
        if (-not $Quiet.IsPresent) {
            Write-Host 'Stopping Kestrun server...' -NoNewline
        }

        # If NoWait is specified, return immediately
        if ($NoWait.IsPresent) {
            return
        }

        while ($Server.IsRunning) {
            Start-Sleep -Seconds 1
            if (-not $Quiet.IsPresent) {
                Write-Host '#' -NoNewline
            }
        }

        [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)

        if (-not $Quiet.IsPresent) {
            Write-Host 'Kestrun server stopped.'
        }
    }
}