| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Creates a grouped route context (prefix + shared options) for nested Add-KrMapRoute calls. |
| | 4 | | .DESCRIPTION |
| | 5 | | While the ScriptBlock runs, all Add-KrMapRoute calls inherit: |
| | 6 | | - Prefix (prepended to -Path) |
| | 7 | | - AuthorizationSchema / AuthorizationPolicy |
| | 8 | | - ExtraImports / ExtraRefs |
| | 9 | | - Arguments (merged; child overrides keys) |
| | 10 | | Supports nesting; inner groups inherit and can override unless -NoInherit is used. |
| | 11 | | .PARAMETER Server |
| | 12 | | The Kestrun server instance to which the route will be added. |
| | 13 | | If not specified, the function will attempt to resolve the current server context. |
| | 14 | | .PARAMETER Options |
| | 15 | | The options to apply to all routes in the group. |
| | 16 | | .PARAMETER Prefix |
| | 17 | | The path prefix for the group (e.g. '/todoitems'). |
| | 18 | | .PARAMETER AuthorizationSchema |
| | 19 | | Authorization schemes required by all routes in the group. |
| | 20 | | .PARAMETER AuthorizationPolicy |
| | 21 | | Authorization policies required by all routes in the group. |
| | 22 | | .PARAMETER ExtraImports |
| | 23 | | Extra namespaces added to all routes in the group. |
| | 24 | | .PARAMETER ExtraRefs |
| | 25 | | Extra assemblies referenced by all routes in the group. |
| | 26 | | .PARAMETER Arguments |
| | 27 | | Extra arguments injected into all routes in the group. |
| | 28 | | .PARAMETER ScriptBlock |
| | 29 | | ScriptBlock within which you call Add-KrMapRoute for relative paths. |
| | 30 | | .PARAMETER FileName |
| | 31 | | Path to a script file containing the scriptblock to execute. |
| | 32 | | .PARAMETER NoInherit |
| | 33 | | If set, do not inherit options from the parent group; only apply the current parameters. |
| | 34 | | .PARAMETER PassThru |
| | 35 | | If specified, the function will return the created route object. |
| | 36 | | .EXAMPLE |
| | 37 | | Add-KrRouteGroup -Prefix '/todoitems' -AuthorizationPolicy 'RequireUser' -ScriptBlock { |
| | 38 | | Add-KrMapRoute -Verbs Get -Path '/' -ScriptBlock { 'all todos' } |
| | 39 | | Add-KrMapRoute -Verbs Get -Path '/{id}' -ScriptBlock { "todo $($Context.Request.RouteValues['id'])" } |
| | 40 | | Add-KrMapRoute -Verbs Post -Path '/' -ScriptBlock { write-KrResponse -InputObject 'create' } |
| | 41 | | } |
| | 42 | | Adds a new route group to the specified Kestrun server with the given prefix and options. |
| | 43 | | .EXAMPLE |
| | 44 | | Add-KrRouteGroup -Prefix '/todoitems' -FileName 'C:\Scripts\TodoItems.ps1' |
| | 45 | | Add the new route group defined in the specified file. |
| | 46 | | .NOTES |
| | 47 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | 48 | | #> |
| | 49 | | function Add-KrRouteGroup { |
| | 50 | | [KestrunRuntimeApi('Definition')] |
| | 51 | | [CmdletBinding(DefaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)] |
| | 52 | | param( |
| | 53 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 54 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 55 | |
|
| | 56 | | [Parameter(Mandatory, ParameterSetName = 'ScriptBlockWithOptions')] |
| | 57 | | [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')] |
| | 58 | | [Kestrun.Hosting.Options.MapRouteOptions]$Options, |
| | 59 | |
|
| | 60 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 61 | | [Parameter(ParameterSetName = 'FileName')] |
| | 62 | | [string]$Prefix, |
| | 63 | |
|
| | 64 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 65 | | [Parameter(ParameterSetName = 'FileName')] |
| | 66 | | [string[]]$AuthorizationSchema, |
| | 67 | |
|
| | 68 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 69 | | [Parameter(ParameterSetName = 'FileName')] |
| | 70 | | [string[]]$AuthorizationPolicy, |
| | 71 | |
|
| | 72 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 73 | | [Parameter(ParameterSetName = 'FileName')] |
| | 74 | | [string[]]$ExtraImports, |
| | 75 | |
|
| | 76 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 77 | | [Parameter(ParameterSetName = 'FileName')] |
| | 78 | | [System.Reflection.Assembly[]]$ExtraRefs, |
| | 79 | |
|
| | 80 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | 81 | | [Parameter(ParameterSetName = 'FileName')] |
| | 82 | | [hashtable]$Arguments, |
| | 83 | |
|
| | 84 | | [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlock')] |
| | 85 | | [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlockWithOptions')] |
| | 86 | | [scriptblock]$ScriptBlock, |
| | 87 | |
|
| | 88 | | [Parameter(Mandatory, ParameterSetName = 'FileName')] |
| | 89 | | [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')] |
| | 90 | | [string]$FileName, |
| | 91 | |
|
| | 92 | | [Parameter()] |
| | 93 | | [switch]$NoInherit, |
| | 94 | |
|
| | 95 | | [Parameter()] |
| | 96 | | [switch]$PassThru |
| | 97 | | ) |
| | 98 | | begin { |
| | 99 | | # Ensure the server instance is resolved |
| 1 | 100 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 101 | | if ($null -eq $Server) { |
| 0 | 102 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 103 | | } |
| | 104 | | } |
| | 105 | | process { |
| | 106 | |
|
| 1 | 107 | | if ($PSCmdlet.ParameterSetName -like 'FileName*') { |
| 0 | 108 | | if (-not (Test-Path -Path $FileName)) { |
| 0 | 109 | | throw "The specified file path does not exist: $FileName" |
| | 110 | | } |
| 0 | 111 | | $code = Get-Content -Path $FileName -Raw |
| 0 | 112 | | $ScriptBlock = [scriptblock]::Create($code) |
| | 113 | | } |
| | 114 | |
|
| | 115 | | # Normalize prefix: allow "todoitems" or "/todoitems" |
| 1 | 116 | | if (-not [string]::IsNullOrWhiteSpace($Prefix) -and -not $Prefix.StartsWith('/')) { |
| 0 | 117 | | $Prefix = "/$Prefix" |
| | 118 | | } |
| | 119 | | # Discover parent template (stack always aims to hold MapRouteOptions) |
| 1 | 120 | | $parentOpts = $null |
| 1 | 121 | | if (-not $NoInherit -and $server.RouteGroupStack.Count) { |
| 1 | 122 | | $parentOpts = $server.RouteGroupStack.Peek() |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | $curr = if ($PSCmdlet.ParameterSetName -like '*WithOptions') { |
| 0 | 126 | | $Options |
| | 127 | | } else { |
| 1 | 128 | | $dict = $null |
| 1 | 129 | | if ($Arguments) { |
| 0 | 130 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| 0 | 131 | | foreach ($k in $Arguments.Keys) { $dict[$k] = $Arguments[$k] } |
| | 132 | | } |
| 1 | 133 | | New-KrMapRouteOption -Property @{ |
| 1 | 134 | | RequireSchemes = $AuthorizationSchema |
| 1 | 135 | | RequirePolicies = $AuthorizationPolicy |
| 1 | 136 | | ExtraImports = $ExtraImports |
| 1 | 137 | | ExtraRefs = $ExtraRefs |
| 1 | 138 | | Arguments = $dict |
| 1 | 139 | | Pattern = $Prefix |
| | 140 | | } |
| | 141 | | } |
| | 142 | | # Merge with parent (parent first, then current overrides) |
| 1 | 143 | | if ($parentOpts) { |
| 1 | 144 | | $curr = _KrMerge-MRO -Parent $parentOpts -Child $curr |
| | 145 | | } |
| | 146 | |
|
| 1 | 147 | | $Server.RouteGroupStack.Push($curr) |
| | 148 | | try { |
| 1 | 149 | | & $ScriptBlock |
| | 150 | | } catch { |
| 0 | 151 | | $msg = "Error inside route group '$($current.Prefix)': $($_.Exception.Message)" |
| 0 | 152 | | throw [System.Exception]::new($msg, $_.Exception) |
| | 153 | | } finally { |
| 1 | 154 | | $null = $Server.RouteGroupStack.Pop() |
| 1 | 155 | | if ($restorePath) { Set-Location $restorePath } |
| | 156 | | } |
| | 157 | |
|
| 1 | 158 | | if ($PassThru.IsPresent) { |
| | 159 | | # if the PassThru switch is specified, return the server instance |
| | 160 | | # Return the modified server instance |
| 0 | 161 | | return $Server |
| | 162 | | } |
| | 163 | | } |
| | 164 | | } |
| | 165 | |
|