| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Enables Kestrun server configuration and starts the server. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function applies the configuration to the Kestrun server and starts it. |
| | 6 | | .PARAMETER Server |
| | 7 | | The Kestrun server instance to configure and start. This parameter is mandatory. |
| | 8 | | .PARAMETER ExcludeVariables |
| | 9 | | An array of variable names to exclude from the runspaces. |
| | 10 | | .PARAMETER ExcludeFunctions |
| | 11 | | An array of function name patterns to exclude from the runspaces. |
| | 12 | | .PARAMETER Quiet |
| | 13 | | If specified, suppresses output messages during the configuration and startup process. |
| | 14 | | .PARAMETER PassThru |
| | 15 | | If specified, the cmdlet will return the modified server instance after applying the configuration. |
| | 16 | | .EXAMPLE |
| | 17 | | Enable-KrConfiguration -Server $server |
| | 18 | | Applies the configuration to the specified Kestrun server instance and starts it. |
| | 19 | | .NOTES |
| | 20 | | This function is designed to be used after the server has been configured with routes, listeners, |
| | 21 | | and other middleware components. |
| | 22 | | #> |
| | 23 | | function Enable-KrConfiguration { |
| | 24 | | [KestrunRuntimeApi('Definition')] |
| | 25 | | [CmdletBinding()] |
| | 26 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 27 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')] |
| | 28 | | param( |
| | 29 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 30 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 31 | | [Parameter()] |
| | 32 | | [string[]]$ExcludeVariables, |
| | 33 | | [Parameter()] |
| | 34 | | [string[]]$ExcludeFunctions, |
| | 35 | | [Parameter()] |
| | 36 | | [switch]$Quiet, |
| | 37 | | [Parameter()] |
| | 38 | | [switch]$PassThru |
| | 39 | | ) |
| | 40 | | begin { |
| | 41 | | # Ensure the server instance is resolved |
| 1 | 42 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 43 | | if ($null -eq $Server) { |
| 0 | 44 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 45 | | } |
| | 46 | | } |
| | 47 | | process { |
| 1 | 48 | | $Variables = Get-KrAssignedVariable -FromParent -ResolveValues -IncludeSetVariable |
| | 49 | |
|
| | 50 | | # Build user variable map as before |
| 1 | 51 | | $vars = [System.Collections.Generic.Dictionary[string, object]]::new() |
| 1 | 52 | | foreach ($v in $Variables) { |
| 1 | 53 | | if ($ExcludeVariables -notcontains $v.Name) { |
| 1 | 54 | | if (-not [Kestrun.SharedState.SharedStateStore]::Contains($v.Name)) { |
| 1 | 55 | | [void][Kestrun.SharedState.SharedStateStore]::Set($v.Name, $v.Value.Value, $true) |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| 1 | 60 | | $callerPath = $null |
| 1 | 61 | | $selfPath = $PSCommandPath |
| 1 | 62 | | foreach ($f in Get-PSCallStack) { |
| 1 | 63 | | $p = $f.InvocationInfo.ScriptName |
| 1 | 64 | | if ($p) { |
| 2 | 65 | | $rp = (Resolve-Path -LiteralPath $p -ErrorAction SilentlyContinue)?.ProviderPath |
| 3 | 66 | | if ($rp -and (!$selfPath -or $rp -ne (Resolve-Path -LiteralPath $selfPath).ProviderPath)) { |
| 1 | 67 | | $callerPath = $rp |
| | 68 | | break |
| | 69 | | } |
| | 70 | | } |
| | 71 | | } |
| | 72 | | # AUTO: collect session-defined functions present now |
| 3 | 73 | | $fx = Get-Command -CommandType Function | Where-Object { -not $_.Module } |
| 1 | 74 | | if ($OnlyCurrentScript -and $callerPath) { |
| 0 | 75 | | $fx = $fx | Where-Object { |
| 0 | 76 | | $_.ScriptBlock.File -and |
| 0 | 77 | | ((Resolve-Path -LiteralPath $_.ScriptBlock.File -ErrorAction SilentlyContinue)?.ProviderPath) -eq $calle |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | # Exclude functions by name patterns if specified |
| 1 | 82 | | if ($ExcludeFunctions) { |
| 0 | 83 | | $fx = $fx | Where-Object { |
| 0 | 84 | | $n = $_.Name |
| 0 | 85 | | -not ($ExcludeFunctions | Where-Object { $n -like $_ }).Count |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | # Create a dictionary of function names to their definitions |
| 1 | 90 | | $fxMap = $null |
| 1 | 91 | | if ($fx.Count) { |
| 1 | 92 | | $fxMap = [System.Collections.Generic.Dictionary[string, string]]::new([System.StringComparer]::OrdinalIgnore |
| 2 | 93 | | foreach ($f in $fx) { $fxMap[$f.Name] = $f.Definition } |
| | 94 | | } |
| | 95 | | # Apply the configuration to the server |
| | 96 | | # Set the user-defined variables in the server configuration |
| 2 | 97 | | $Server.EnableConfiguration($vars, $fxMap) | Out-Null |
| 1 | 98 | | if (-not $Quiet.IsPresent) { |
| 0 | 99 | | Write-Host 'Kestrun server configuration enabled successfully.' |
| 0 | 100 | | Write-Host "Server Name: $($Server.Options.ApplicationName)" |
| | 101 | | } |
| | 102 | |
|
| 1 | 103 | | if ($PassThru.IsPresent) { |
| | 104 | | # if the PassThru switch is specified, return the server instance |
| | 105 | | # Return the modified server instance |
| 0 | 106 | | return $Server |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |