< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
38%
Covered lines: 8
Uncovered lines: 13
Coverable lines: 21
Total lines: 82
Line coverage: 38%
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/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 PassThru
 17        If specified, the cmdlet will return the created server instance.
 18    .PARAMETER Force
 19        If specified, the cmdlet will overwrite any existing server instance with the same name.
 20    .EXAMPLE
 21        New-KrServer -Name "MyKestrunServer"
 22        Creates a new Kestrun server instance with the specified name.
 23    .NOTES
 24        This function is designed to be used in the context of a Kestrun server setup.
 25#>
 26function New-KrServer {
 27    [KestrunRuntimeApi('Definition')]
 28    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 29    [CmdletBinding(DefaultParameterSetName = 'Logger')]
 30    [OutputType([Kestrun.Hosting.KestrunHost])]
 31    param(
 32        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
 33        [string]$Name,
 34        [Parameter(Mandatory = $false, ParameterSetName = 'Logger')]
 35        [Serilog.ILogger]$Logger,
 36        [Parameter(Mandatory = $true, ParameterSetName = 'LoggerName')]
 37        [string]$LoggerName,
 38        [Parameter()]
 39        [switch]$PassThru,
 40        [Parameter()]
 41        [switch]$Force
 42    )
 43    process {
 144        $loadedModules = Get-KrUserImportedModule
 445        $modulePaths = @($loadedModules | ForEach-Object { $_.Path })
 146        if ([Kestrun.KestrunHostManager]::Contains($Name) ) {
 047            if ($Force) {
 048                if ([Kestrun.KestrunHostManager]::IsRunning($Name)) {
 049                    [Kestrun.KestrunHostManager]::Stop($Name)
 50                }
 051                [Kestrun.KestrunHostManager]::Destroy($Name)
 52            } else {
 053                $confirm = Read-Host "Server '$Name' is running. Do you want to stop and destroy the previous instance? 
 054                if ($confirm -notin @('Y', 'y')) {
 055                    Write-Warning 'Operation cancelled by user.'
 056                    exit 1
 57                }
 058                if ([Kestrun.KestrunHostManager]::IsRunning($Name)) {
 059                    [Kestrun.KestrunHostManager]::Stop($Name)
 60                }
 061                [Kestrun.KestrunHostManager]::Destroy($Name)
 62            }
 63        }
 64
 65        # If Logger is not provided, use the default logger or the named logger
 166        if ($Null -eq $Logger) {
 167            if ([string]::IsNullOrEmpty($LoggerName)) {
 168                $Logger = [Serilog.Log]::Logger
 69            } else {
 70                # If LoggerName is specified, get the logger with that name
 071                $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName)
 72            }
 73        }
 74
 175        $server = [Kestrun.KestrunHostManager]::Create($Name, $Logger, [string[]] $modulePaths)
 176        if ($PassThru.IsPresent) {
 77            # if the PassThru switch is specified, return the server instance
 78            # Return the modified server instance
 079            return $Server
 80        }
 81    }
 82}

Methods/Properties

New-KrServer()