| | | 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 | | #> |
| | | 39 | | function 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 |
| | 0 | 65 | | if ($Environment -ne 'Auto') { |
| | 0 | 66 | | Set-KrEnvironment -Name $Environment | Out-Null |
| | | 67 | | } else { |
| | | 68 | | # Auto: if debugger-ish, become Development; else clear override |
| | 0 | 69 | | if (Test-KrDebugContext) { |
| | 0 | 70 | | Set-KrEnvironment -Name Development | Out-Null |
| | | 71 | | } else { |
| | 0 | 72 | | Set-KrEnvironment -Name Auto | Out-Null |
| | | 73 | | } |
| | | 74 | | } |
| | 0 | 75 | | Write-Verbose ('Kestrun environment -> ' + (Get-KrEnvironment)) |
| | | 76 | | } |
| | | 77 | | process { |
| | 0 | 78 | | $loadedModules = Get-KrUserImportedModule |
| | 0 | 79 | | $modulePaths = @($loadedModules | ForEach-Object { $_.Path }) |
| | 0 | 80 | | if ([Kestrun.KestrunHostManager]::Contains($Name) ) { |
| | 0 | 81 | | if ($Force) { |
| | 0 | 82 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| | 0 | 83 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | | 84 | | } |
| | 0 | 85 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | | 86 | | } else { |
| | 0 | 87 | | $confirm = Read-Host "Server '$Name' is running. Do you want to stop and destroy the previous instance? |
| | 0 | 88 | | if ($confirm -notin @('Y', 'y')) { |
| | 0 | 89 | | Write-Warning 'Operation cancelled by user.' |
| | 0 | 90 | | exit 1 |
| | | 91 | | } |
| | 0 | 92 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| | 0 | 93 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | | 94 | | } |
| | 0 | 95 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | # If Logger is not provided, use the default logger or the named logger |
| | 0 | 100 | | if ($Null -eq $Logger) { |
| | 0 | 101 | | if ([string]::IsNullOrEmpty($LoggerName)) { |
| | 0 | 102 | | $Logger = [Serilog.Log]::Logger |
| | | 103 | | } else { |
| | | 104 | | # If LoggerName is specified, get the logger with that name |
| | 0 | 105 | | $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName) |
| | | 106 | | } |
| | | 107 | | } |
| | 0 | 108 | | $enablePowershellMiddleware = -not $DisablePowershellMiddleware.IsPresent |
| | | 109 | | |
| | 0 | 110 | | $server = [Kestrun.KestrunHostManager]::Create($Name, $Logger, [string[]] $modulePaths, $Default.IsPresent, $ena |
| | 0 | 111 | | if ($PassThru.IsPresent) { |
| | | 112 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 113 | | return $Server |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |