| | | 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 { |
| | 0 | 41 | | $effectiveQuiet = $Quiet.IsPresent |
| | 0 | 42 | | if (-not $PSBoundParameters.ContainsKey('Quiet')) { |
| | 0 | 43 | | $effectiveQuiet = [bool]$ExecutionContext.SessionState.PSVariable.GetValue('__krRunnerQuiet', $false) |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | # Ensure the server instance is resolved |
| | 0 | 47 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 48 | | } |
| | | 49 | | process { |
| | | 50 | | # Collect assigned variables from the parent scope, resolving their values |
| | 0 | 51 | | $variables = Get-KrAssignedVariable -FromParent -ResolveValues -IncludeSetVariable -ExcludeVariables $ExcludeVar |
| | | 52 | | |
| | 0 | 53 | | Write-KrLog -Level Debug -Logger $Server.Logger -Message 'Collected {VarCount} user-defined variables for server |
| | | 54 | | |
| | | 55 | | # AUTO: determine caller script path to filter session-defined functions |
| | 0 | 56 | | $callerPath = [Kestrun.KestrunHostManager]::EntryScriptPath |
| | 0 | 57 | | if ([string]::IsNullOrEmpty($callerPath)) { |
| | 0 | 58 | | throw 'KestrunHostManager is not properly initialized. EntryScriptPath is not set.' |
| | | 59 | | } |
| | 0 | 60 | | $callerPath = (Resolve-Path -LiteralPath $callerPath -ErrorAction Stop).ProviderPath |
| | 0 | 61 | | $callerDirectory = Split-Path -Parent $callerPath |
| | 0 | 62 | | $callerDirectoryPrefix = if ([string]::IsNullOrWhiteSpace($callerDirectory)) { |
| | 0 | 63 | | $null |
| | | 64 | | } else { |
| | 0 | 65 | | $callerDirectory.TrimEnd('\', '/') + [System.IO.Path]::DirectorySeparatorChar |
| | | 66 | | } |
| | | 67 | | # AUTO: collect session-defined functions present now |
| | 0 | 68 | | $fx = @( Get-Command -CommandType Function | Where-Object { -not $_.Module } ) |
| | | 69 | | |
| | 0 | 70 | | if ($callerPath) { |
| | 0 | 71 | | $fx = $fx | Where-Object { |
| | 0 | 72 | | if (-not $_.ScriptBlock.File) { |
| | 0 | 73 | | return $false |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | $functionPath = (Resolve-Path -LiteralPath $_.ScriptBlock.File -ErrorAction SilentlyContinue)?.ProviderP |
| | 0 | 77 | | if (-not $functionPath) { |
| | 0 | 78 | | return $false |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | if ($functionPath -eq $callerPath) { |
| | 0 | 82 | | return $true |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | if ([string]::IsNullOrWhiteSpace($callerDirectoryPrefix)) { |
| | 0 | 86 | | return $false |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | return $functionPath.StartsWith($callerDirectoryPrefix, [System.StringComparison]::OrdinalIgnoreCase) |
| | | 90 | | } |
| | 0 | 91 | | $fx = @($fx) # normalize again |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | # Exclude functions by name patterns if specified |
| | 0 | 95 | | if ($ExcludeFunctions) { |
| | 0 | 96 | | $fx = $fx | Where-Object { |
| | 0 | 97 | | $n = $_.Name |
| | 0 | 98 | | -not ($ExcludeFunctions | Where-Object { $n -like $_ }).Count |
| | | 99 | | } |
| | 0 | 100 | | $fx = @($fx) # normalize again |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | # Create a dictionary of function names to their definitions |
| | | 104 | | # NOTE: exclude OpenAPI metadata functions (OpenApiPath/OpenApiWebhook/OpenApiCallback) |
| | | 105 | | # from the general function map; they are handled separately. |
| | 0 | 106 | | $fxMap = $null |
| | 0 | 107 | | if ($fx -and $fx.Count -gt 0) { |
| | 0 | 108 | | $fxUser = @($fx | Where-Object { -not (Test-KrFunctionHasAttribute -Command $_ -AttributeNameRegex 'OpenApi( |
| | 0 | 109 | | $fxMap = [System.Collections.Generic.Dictionary[string, string]]::new([System.StringComparer]::OrdinalIgnore |
| | 0 | 110 | | foreach ($f in $fxUser) { $fxMap[$f.Name] = $f.Definition } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | # Create a dictionary of OpenAPI callback function names to their definitions |
| | 0 | 114 | | $fxCallBack = $null |
| | 0 | 115 | | if ($fx -and $fx.Count -gt 0) { |
| | 0 | 116 | | $cb = @($fx | Where-Object { Test-KrFunctionHasAttribute -Command $_ -AttributeNameRegex 'OpenApiCallback' } |
| | 0 | 117 | | if ($cb -and $cb.Count -gt 0) { |
| | 0 | 118 | | $fxCallBack = [System.Collections.Generic.Dictionary[string, string]]::new([System.StringComparer]::Ordi |
| | 0 | 119 | | foreach ($f in $cb) { $fxCallBack[$f.Name] = $f.Definition } |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | if ($Server.Logger -and $Server.Logger.IsEnabled([Serilog.Events.LogEventLevel]::Debug)) { |
| | 0 | 124 | | $callbackNames = @() |
| | 0 | 125 | | if ($fxCallBack) { |
| | 0 | 126 | | $callbackNames = @($fxCallBack.Keys) |
| | 0 | 127 | | [System.Array]::Sort($callbackNames, [System.StringComparer]::OrdinalIgnoreCase) |
| | | 128 | | } |
| | 0 | 129 | | Write-KrLog -Level Debug -Logger $Server.Logger -Message 'Detected {CallbackCount} OpenAPI callback function |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | Write-KrLog -Level Debug -Logger $Server.Logger -Message 'Enabling Kestrun server configuration with {VarCount} |
| | | 133 | | # Apply the configuration to the server |
| | | 134 | | # Set the user-defined variables in the server configuration |
| | 0 | 135 | | $Server.EnableConfiguration($variables, $fxMap, $fxCallBack) | Out-Null |
| | | 136 | | |
| | 0 | 137 | | Write-KrLog -Level Information -Logger $Server.Logger -Message 'Kestrun server configuration enabled successfull |
| | | 138 | | |
| | 0 | 139 | | if (-not $effectiveQuiet) { |
| | 0 | 140 | | Write-Host 'Kestrun server configuration enabled successfully.' |
| | 0 | 141 | | Write-Host "Server Name: $($Server.Options.ApplicationName)" |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | if ($PassThru.IsPresent) { |
| | | 145 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 146 | | return $Server |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |