< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.New-KrServer
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/New-KrServer.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 116
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: 29.4% (5/17) Total lines: 63 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 18:11:31 Line coverage: 29.4% (5/17) Total lines: 64 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/06/2025 - 02:59:55 Line coverage: 31.5% (6/19) Total lines: 76 Tag: Kestrun/Kestrun@2111f41add084358558c4f74e28884318c1923b009/07/2025 - 04:51:39 Line coverage: 38% (8/21) Total lines: 82 Tag: Kestrun/Kestrun@461ff737fcae3442df54fb34b135b2349f239c4f10/13/2025 - 16:52:37 Line coverage: 0% (0/28) Total lines: 116 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Creates a new Kestrun server instance.
 4    .DESCRIPTION
 5        This function initializes a new Kestrun server instance with the specified name and logger.
 6    .PARAMETER Name
 7        The name of the Kestrun server instance to create.
 8    .PARAMETER Logger
 9        An optional Serilog logger instance to use for logging.
 10        It's mutually exclusive with the LoggerName parameter.
 11        If not specified, the default logger will be used.
 12    .PARAMETER LoggerName
 13        An optional name of a registered logger to use for logging.
 14        It's mutually exclusive with the Logger parameter.
 15        If specified, the logger with this name will be used instead of the default logger.
 16    .PARAMETER DisablePowershellMiddleware
 17        If specified, the PowerShell middleware will be disabled for this server instance.
 18    .PARAMETER Default
 19        If specified, this server instance will be set as the default instance.
 20    .PARAMETER PassThru
 21        If specified, the cmdlet will return the created server instance.
 22    .PARAMETER Environment
 23        The environment to set for the Kestrun server instance. Valid values are 'Auto', 'Development', 'Staging', and '
 24        - 'Auto' (default): Automatically sets the environment to 'Development' if a debugger is attached or
 25            if the -Debug switch is used; otherwise, it uses the environment specified by the KESTRUN_ENVIRONMENT enviro
 26            or defaults to 'Production'.
 27        - 'Development': Forces the environment to 'Development'.
 28        - 'Staging': Forces the environment to 'Staging'.
 29        - 'Production': Forces the environment to 'Production'.
 30        The environment setting affects middleware behavior, such as detailed error pages in 'Development'.
 31    .PARAMETER Force
 32        If specified, the cmdlet will overwrite any existing server instance with the same name.
 33    .EXAMPLE
 34        New-KrServer -Name "MyKestrunServer"
 35        Creates a new Kestrun server instance with the specified name.
 36    .NOTES
 37        This function is designed to be used in the context of a Kestrun server setup.
 38#>
 39function New-KrServer {
 40    [KestrunRuntimeApi('Definition')]
 41    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 42    [CmdletBinding(DefaultParameterSetName = 'Logger')]
 43    [OutputType([Kestrun.Hosting.KestrunHost])]
 44    param(
 45        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
 46        [string]$Name,
 47        [Parameter(Mandatory = $false, ParameterSetName = 'Logger')]
 48        [Serilog.ILogger]$Logger,
 49        [Parameter(Mandatory = $true, ParameterSetName = 'LoggerName')]
 50        [string]$LoggerName,
 51        [Parameter()]
 52        [switch]$PassThru,
 53        [Parameter()]
 54        [switch]$DisablePowershellMiddleware,
 55        [Parameter()]
 56        [switch]$Default,
 57        [Parameter()]
 58        [ValidateSet('Auto', 'Development', 'Staging', 'Production')]
 59        [string]$Environment = 'Auto',
 60        [Parameter()]
 61        [switch]$Force
 62    )
 63    begin {
 64        # Honor explicit -Environment if provided
 065        if ($Environment -ne 'Auto') {
 066            Set-KrEnvironment -Name $Environment | Out-Null
 67        } else {
 68            # Auto: if debugger-ish, become Development; else clear override
 069            if (Test-KrDebugContext) {
 070                Set-KrEnvironment -Name Development | Out-Null
 71            } else {
 072                Set-KrEnvironment -Name Auto | Out-Null
 73            }
 74        }
 075        Write-Verbose ('Kestrun environment -> ' + (Get-KrEnvironment))
 76    }
 77    process {
 078        $loadedModules = Get-KrUserImportedModule
 079        $modulePaths = @($loadedModules | ForEach-Object { $_.Path })
 080        if ([Kestrun.KestrunHostManager]::Contains($Name) ) {
 081            if ($Force) {
 082                if ([Kestrun.KestrunHostManager]::IsRunning($Name)) {
 083                    [Kestrun.KestrunHostManager]::Stop($Name)
 84                }
 085                [Kestrun.KestrunHostManager]::Destroy($Name)
 86            } else {
 087                $confirm = Read-Host "Server '$Name' is running. Do you want to stop and destroy the previous instance? 
 088                if ($confirm -notin @('Y', 'y')) {
 089                    Write-Warning 'Operation cancelled by user.'
 090                    exit 1
 91                }
 092                if ([Kestrun.KestrunHostManager]::IsRunning($Name)) {
 093                    [Kestrun.KestrunHostManager]::Stop($Name)
 94                }
 095                [Kestrun.KestrunHostManager]::Destroy($Name)
 96            }
 97        }
 98
 99        # If Logger is not provided, use the default logger or the named logger
 0100        if ($Null -eq $Logger) {
 0101            if ([string]::IsNullOrEmpty($LoggerName)) {
 0102                $Logger = [Serilog.Log]::Logger
 103            } else {
 104                # If LoggerName is specified, get the logger with that name
 0105                $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName)
 106            }
 107        }
 0108        $enablePowershellMiddleware = -not $DisablePowershellMiddleware.IsPresent
 109
 0110        $server = [Kestrun.KestrunHostManager]::Create($Name, $Logger, [string[]] $modulePaths, $Default.IsPresent, $ena
 0111        if ($PassThru.IsPresent) {
 112            # if the PassThru switch is specified, return the modified server instance
 0113            return $Server
 114        }
 115    }
 116}

Methods/Properties

New-KrServer()