| | 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 | | #> |
| | 26 | | function 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 { |
| 1 | 44 | | $loadedModules = Get-KrUserImportedModule |
| 4 | 45 | | $modulePaths = @($loadedModules | ForEach-Object { $_.Path }) |
| 1 | 46 | | if ([Kestrun.KestrunHostManager]::Contains($Name) ) { |
| 0 | 47 | | if ($Force) { |
| 0 | 48 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| 0 | 49 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | 50 | | } |
| 0 | 51 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | 52 | | } else { |
| 0 | 53 | | $confirm = Read-Host "Server '$Name' is running. Do you want to stop and destroy the previous instance? |
| 0 | 54 | | if ($confirm -notin @('Y', 'y')) { |
| 0 | 55 | | Write-Warning 'Operation cancelled by user.' |
| 0 | 56 | | exit 1 |
| | 57 | | } |
| 0 | 58 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| 0 | 59 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | 60 | | } |
| 0 | 61 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | # If Logger is not provided, use the default logger or the named logger |
| 1 | 66 | | if ($Null -eq $Logger) { |
| 1 | 67 | | if ([string]::IsNullOrEmpty($LoggerName)) { |
| 1 | 68 | | $Logger = [Serilog.Log]::Logger |
| | 69 | | } else { |
| | 70 | | # If LoggerName is specified, get the logger with that name |
| 0 | 71 | | $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName) |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 1 | 75 | | $server = [Kestrun.KestrunHostManager]::Create($Name, $Logger, [string[]] $modulePaths) |
| 1 | 76 | | if ($PassThru.IsPresent) { |
| | 77 | | # if the PassThru switch is specified, return the server instance |
| | 78 | | # Return the modified server instance |
| 0 | 79 | | return $Server |
| | 80 | | } |
| | 81 | | } |
| | 82 | | } |