| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds callback automation middleware to the Kestrun host. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet adds middleware to the Kestrun host that enables automatic handling of callbacks using |
| | | 6 | | specified options or individual parameters for configuration. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun host instance to which the middleware will be added. If not specified, the current host instance wil |
| | | 9 | | .PARAMETER Options |
| | | 10 | | An instance of CallbackDispatchOptions to configure callback dispatch behavior. |
| | | 11 | | .PARAMETER DefaultTimeout |
| | | 12 | | The default timeout in seconds for callback operations. Used when Options is not provided. |
| | | 13 | | .PARAMETER MaxAttempts |
| | | 14 | | The maximum number of attempts for callback operations. Used when Options is not provided. |
| | | 15 | | .PARAMETER BaseDelay |
| | | 16 | | The base delay in seconds between callback attempts. Used when Options is not provided. |
| | | 17 | | .PARAMETER MaxDelay |
| | | 18 | | The maximum delay in seconds between callback attempts. Used when Options is not provided. |
| | | 19 | | .PARAMETER PassThru |
| | | 20 | | If specified, the cmdlet returns the modified Kestrun host instance. |
| | | 21 | | .EXAMPLE |
| | | 22 | | PS> Add-KrAddCallbacksAutomation -DefaultTimeout 30 -MaxAttempts 5 -BaseDelay 2 -MaxDelay 60 |
| | | 23 | | Adds callback automation middleware to the current Kestrun host with specified parameters and returns the modifi |
| | | 24 | | .EXAMPLE |
| | | 25 | | PS> $server = Get-KrServer |
| | | 26 | | PS> Add-KrAddCallbacksAutomation -Server $server -Options $customOptions |
| | | 27 | | Adds callback automation middleware to the specified Kestrun host using the provided options. |
| | | 28 | | .NOTES |
| | | 29 | | This cmdlet is part of the Kestrun PowerShell module. |
| | | 30 | | #> |
| | | 31 | | function Add-KrAddCallbacksAutomation { |
| | | 32 | | [KestrunRuntimeApi('Definition')] |
| | | 33 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 34 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 35 | | param( |
| | | 36 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 37 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 38 | | |
| | | 39 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 40 | | [Kestrun.Callback.CallbackDispatchOptions]$Options, |
| | | 41 | | |
| | | 42 | | [Parameter(ParameterSetName = 'Items')] |
| | | 43 | | [ValidateRange(1, 600)] |
| | | 44 | | [int]$DefaultTimeout, |
| | | 45 | | |
| | | 46 | | [Parameter(ParameterSetName = 'Items')] |
| | | 47 | | [ValidateRange(1, 99)] |
| | | 48 | | [int]$MaxAttempts, |
| | | 49 | | |
| | | 50 | | [Parameter(ParameterSetName = 'Items')] |
| | | 51 | | [ValidateRange(1, 30)] |
| | | 52 | | [int]$BaseDelay, |
| | | 53 | | |
| | | 54 | | [Parameter(ParameterSetName = 'Items')] |
| | | 55 | | [ValidateRange(1, 300)] |
| | | 56 | | [int]$MaxDelay, |
| | | 57 | | |
| | | 58 | | [Parameter()] |
| | | 59 | | [switch]$PassThru |
| | | 60 | | ) |
| | | 61 | | begin { |
| | | 62 | | # Ensure the server instance is resolved |
| | 0 | 63 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 64 | | } |
| | | 65 | | process { |
| | 0 | 66 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 67 | | # Create options from individual parameters |
| | 0 | 68 | | $Options = [Kestrun.Callback.CallbackDispatchOptions]::new() |
| | | 69 | | # Set default values |
| | 0 | 70 | | if ($PSBoundParameters.ContainsKey('DefaultTimeout')) { |
| | 0 | 71 | | $Options.DefaultTimeout = [TimeSpan]::FromSeconds($DefaultTimeout) |
| | | 72 | | } |
| | 0 | 73 | | if ($PSBoundParameters.ContainsKey('MaxAttempts')) { |
| | 0 | 74 | | $Options.MaxAttempts = $MaxAttempts |
| | | 75 | | } |
| | 0 | 76 | | if ($PSBoundParameters.ContainsKey('BaseDelay')) { |
| | 0 | 77 | | $Options.BaseDelay = [TimeSpan]::FromSeconds($BaseDelay) |
| | | 78 | | } |
| | 0 | 79 | | if ($PSBoundParameters.ContainsKey('MaxDelay')) { |
| | 0 | 80 | | $Options.MaxDelay = [TimeSpan]::FromSeconds($MaxDelay) |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | # Add the callback automation middleware to the server |
| | 0 | 85 | | $Server.AddCallbacksAutomation($Options) | Out-Null |
| | | 86 | | |
| | 0 | 87 | | if ($PassThru.IsPresent) { |
| | | 88 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 89 | | return $Server |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |