| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a new map route to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function allows you to add a new map route to the Kestrun server by specifying the route path and the scrip |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to which the route will be added. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER Options |
| | | 10 | | An instance of `Kestrun.Hosting.Options.MapRouteOptions` that contains the configuration for the route. |
| | | 11 | | This parameter is used to specify various options for the route, such as HTTP verbs, path, authorization schemes |
| | | 12 | | .PARAMETER Verbs |
| | | 13 | | Alias: Method |
| | | 14 | | The HTTP verbs (GET, POST, etc.) that the route should respond to. Defaults to GET. |
| | | 15 | | .PARAMETER Pattern |
| | | 16 | | Alias: Path |
| | | 17 | | The URL path for the new route. |
| | | 18 | | .PARAMETER ScriptBlock |
| | | 19 | | The script block to be executed when the route is accessed. |
| | | 20 | | .PARAMETER Code |
| | | 21 | | The code to be executed when the route is accessed, used in conjunction with the Language parameter. |
| | | 22 | | .PARAMETER Language |
| | | 23 | | The scripting language of the code to be executed. |
| | | 24 | | .PARAMETER CodeFilePath |
| | | 25 | | The file path to the code to be executed when the route is accessed. |
| | | 26 | | .PARAMETER AuthorizationSchema |
| | | 27 | | An optional array of authorization schemes that the route requires. |
| | | 28 | | .PARAMETER AuthorizationPolicy |
| | | 29 | | An optional array of authorization policies that the route requires. |
| | | 30 | | .PARAMETER ExtraImports |
| | | 31 | | An optional array of additional namespaces to import for the route. |
| | | 32 | | .PARAMETER ExtraRefs |
| | | 33 | | An optional array of additional assemblies to reference for the route. |
| | | 34 | | .PARAMETER AllowDuplicate |
| | | 35 | | If specified, allows the addition of duplicate routes with the same path and HTTP verb. |
| | | 36 | | .PARAMETER Arguments |
| | | 37 | | An optional hashtable of arguments to pass to the script block or code. |
| | | 38 | | .PARAMETER Endpoints |
| | | 39 | | An optional array of endpoint names to which the route should be bound. |
| | | 40 | | If not specified, the route will be bound to all endpoints. |
| | | 41 | | .PARAMETER DuplicateAction |
| | | 42 | | Specifies the action to take if a duplicate route is detected. Options are 'Throw', 'Skip', 'Allow', or 'Warn'. |
| | | 43 | | Default is 'Throw', which will raise an error if a duplicate route is found. |
| | | 44 | | .PARAMETER PassThru |
| | | 45 | | If specified, the function will return the created route object. |
| | | 46 | | .OUTPUTS |
| | | 47 | | Returns the Kestrun server instance with the new route added. |
| | | 48 | | .EXAMPLE |
| | | 49 | | Add-KrMapRoute -Server $myServer -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" } |
| | | 50 | | Adds a new map route to the specified Kestrun server with the given path and script block. |
| | | 51 | | .EXAMPLE |
| | | 52 | | |
| | | 53 | | Add-KrMapRoute -Server $myServer -Path "/myroute" -Code "Write-Host 'Hello, World!'" -Language PowerShell |
| | | 54 | | Adds a new map route to the specified Kestrun server with the given path and code. |
| | | 55 | | .EXAMPLE |
| | | 56 | | Get-KrServer | Add-KrMapRoute -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" } -PassThru |
| | | 57 | | Adds a new map route to the current Kestrun server and returns the route object. |
| | | 58 | | .NOTES |
| | | 59 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | | 60 | | #> |
| | | 61 | | function Add-KrMapRoute { |
| | | 62 | | [KestrunRuntimeApi('Definition')] |
| | | 63 | | [CmdletBinding(defaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)] |
| | | 64 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 65 | | param( |
| | | 66 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 67 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 68 | | |
| | | 69 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 70 | | [Kestrun.Hosting.Options.MapRouteOptions]$Options, |
| | | 71 | | |
| | | 72 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 73 | | [Parameter(ParameterSetName = 'Code')] |
| | | 74 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 75 | | [Alias('Method')] |
| | 0 | 76 | | [Kestrun.Utilities.HttpVerb[]]$Verbs = @([Kestrun.Utilities.HttpVerb]::Get), |
| | | 77 | | |
| | | 78 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 79 | | [Parameter(ParameterSetName = 'Code')] |
| | | 80 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 81 | | [ValidatePattern('^/')] |
| | | 82 | | [Alias('Path')] |
| | | 83 | | [string]$Pattern = '/', |
| | | 84 | | |
| | | 85 | | [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')] |
| | | 86 | | [scriptblock]$ScriptBlock, |
| | | 87 | | |
| | | 88 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 89 | | [Alias('CodeBlock')] |
| | | 90 | | [string]$Code, |
| | | 91 | | |
| | | 92 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 93 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 94 | | |
| | | 95 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')] |
| | | 96 | | [string]$CodeFilePath, |
| | | 97 | | |
| | | 98 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 99 | | [Parameter(ParameterSetName = 'Code')] |
| | | 100 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 101 | | [string[]]$AuthorizationSchema = $null, |
| | | 102 | | |
| | | 103 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 104 | | [Parameter(ParameterSetName = 'Code')] |
| | | 105 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 106 | | [string[]]$AuthorizationPolicy = $null, |
| | | 107 | | |
| | | 108 | | [Parameter(ParameterSetName = 'Code')] |
| | | 109 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 110 | | [string[]]$ExtraImports = $null, |
| | | 111 | | |
| | | 112 | | [Parameter(ParameterSetName = 'Code')] |
| | | 113 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 114 | | [System.Reflection.Assembly[]]$ExtraRefs = $null, |
| | | 115 | | |
| | | 116 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 117 | | [Parameter(ParameterSetName = 'Code')] |
| | | 118 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 119 | | [hashtable]$Arguments, |
| | | 120 | | |
| | | 121 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 122 | | [Parameter(ParameterSetName = 'Code')] |
| | | 123 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 124 | | [string[]]$Endpoints, |
| | | 125 | | |
| | | 126 | | [Parameter()] |
| | | 127 | | [switch]$AllowDuplicate, |
| | | 128 | | |
| | | 129 | | [Parameter()] |
| | | 130 | | [ValidateSet('Throw', 'Skip', 'Allow', 'Warn')] |
| | | 131 | | [string]$DuplicateAction = 'Throw', |
| | | 132 | | |
| | | 133 | | [Parameter()] |
| | | 134 | | [switch]$PassThru |
| | | 135 | | ) |
| | | 136 | | begin { |
| | | 137 | | # Ensure the server instance is resolved |
| | 0 | 138 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 139 | | } |
| | | 140 | | process { |
| | | 141 | | |
| | 0 | 142 | | $exists = Test-KrRoute -Path $Pattern -Verb $Verbs |
| | | 143 | | |
| | 0 | 144 | | if ($exists) { |
| | 0 | 145 | | if ($AllowDuplicate -or $DuplicateAction -eq 'Allow') { |
| | 0 | 146 | | Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists; adding another." -Values $ |
| | 0 | 147 | | } elseif ($DuplicateAction -eq 'Skip') { |
| | 0 | 148 | | Write-KrLog -Level Verbose -Message "Route '{Path}' ({Verbs}) exists; skipping." -Values $Pattern, ($Ver |
| | | 149 | | return |
| | 0 | 150 | | } elseif ($DuplicateAction -eq 'Warn') { |
| | 0 | 151 | | Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists." -Values $Pattern, ($Verbs |
| | | 152 | | } else { |
| | 0 | 153 | | throw [System.InvalidOperationException]::new( |
| | 0 | 154 | | "Route '$Pattern' with method(s) $($Verbs -join ',') already exists.") |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | # -- if Options parameter is used, we can skip the rest of the parameters |
| | 0 | 159 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 160 | | $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new() |
| | 0 | 161 | | $Options.HttpVerbs = $Verbs |
| | 0 | 162 | | $Options.Pattern = $Pattern |
| | | 163 | | |
| | 0 | 164 | | if ($null -ne $AuthorizationSchema) { |
| | 0 | 165 | | $Options.RequireSchemes = $AuthorizationSchema |
| | | 166 | | } |
| | 0 | 167 | | if ($null -ne $AuthorizationPolicy) { |
| | 0 | 168 | | $Options.RequirePolicies = $AuthorizationPolicy |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | # Endpoints |
| | 0 | 172 | | if ($null -ne $Endpoints -and $Endpoints.Count -gt 0) { |
| | 0 | 173 | | $Options.Endpoints = $Endpoints |
| | | 174 | | } |
| | | 175 | | # ScriptCode configuration |
| | 0 | 176 | | $Options.ScriptCode.ExtraImports = $ExtraImports |
| | 0 | 177 | | $Options.ScriptCode.ExtraRefs = $ExtraRefs |
| | 0 | 178 | | if ($null -ne $Arguments) { |
| | 0 | 179 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 180 | | foreach ($key in $Arguments.Keys) { |
| | 0 | 181 | | $dict[$key] = $Arguments[$key] |
| | | 182 | | } |
| | 0 | 183 | | $Options.ScriptCode.Arguments = $dict |
| | | 184 | | } |
| | 0 | 185 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 186 | | 'ScriptBlock' { |
| | 0 | 187 | | $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 188 | | $Options.ScriptCode.Code = $ScriptBlock.ToString() |
| | | 189 | | } |
| | | 190 | | 'Code' { |
| | 0 | 191 | | $Options.ScriptCode.Language = $Language |
| | 0 | 192 | | $Options.ScriptCode.Code = $Code |
| | | 193 | | } |
| | | 194 | | 'CodeFilePath' { |
| | 0 | 195 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 196 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 197 | | } |
| | 0 | 198 | | $extension = Split-Path -Path $CodeFilePath -Extension |
| | 0 | 199 | | switch ($extension) { |
| | | 200 | | '.ps1' { |
| | 0 | 201 | | $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 202 | | } |
| | | 203 | | '.cs' { |
| | 0 | 204 | | $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 205 | | } |
| | | 206 | | '.vb' { |
| | 0 | 207 | | $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 208 | | } |
| | | 209 | | default { |
| | 0 | 210 | | throw "Unsupported '$extension' code file extension." |
| | | 211 | | } |
| | | 212 | | } |
| | 0 | 213 | | $Options.ScriptCode.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | } else { |
| | 0 | 217 | | Write-Verbose 'Using provided MapRouteOptions instance.' |
| | | 218 | | } |
| | 0 | 219 | | if ($Server.RouteGroupStack.Count -gt 0) { |
| | 0 | 220 | | $grp = $Server.RouteGroupStack.Peek() |
| | 0 | 221 | | $Options = _KrMerge-MRO -Parent $grp -Child $Options |
| | | 222 | | } |
| | 0 | 223 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddMapRoute($Server, $Options) | Out-Null |
| | | 224 | | |
| | 0 | 225 | | if ($PassThru.IsPresent) { |
| | | 226 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 227 | | return $Server |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |