| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a new HTML template route to the Kestrun server. |
| | | 4 | | |
| | | 5 | | .DESCRIPTION |
| | | 6 | | This function allows you to add a new HTML template route to the Kestrun server by specifying the route path and |
| | | 7 | | |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun server instance to which the route will be added. |
| | | 10 | | If not specified, the function will attempt to resolve the current server context. |
| | | 11 | | |
| | | 12 | | .PARAMETER Pattern |
| | | 13 | | The URL path for the new route. |
| | | 14 | | |
| | | 15 | | .PARAMETER HtmlTemplatePath |
| | | 16 | | The file path to the HTML template to be used for the route. |
| | | 17 | | |
| | | 18 | | .PARAMETER AuthorizationSchema |
| | | 19 | | An optional array of authorization schemes for the route. |
| | | 20 | | |
| | | 21 | | .PARAMETER AuthorizationPolicy |
| | | 22 | | An optional array of authorization policies for the route. |
| | | 23 | | |
| | | 24 | | .PARAMETER PassThru |
| | | 25 | | If specified, the function will return the created route object. |
| | | 26 | | |
| | | 27 | | .OUTPUTS |
| | | 28 | | [Microsoft.AspNetCore.Builder.IEndpointConventionBuilder] representing the created route. |
| | | 29 | | |
| | | 30 | | .EXAMPLE |
| | | 31 | | Add-KrHtmlTemplateRoute -Server $myServer -Path "/myroute" -HtmlTemplatePath "C:\Templates\mytemplate.html" |
| | | 32 | | Adds a new HTML template route to the specified Kestrun server with the given path and template file. |
| | | 33 | | |
| | | 34 | | .EXAMPLE |
| | | 35 | | Get-KrServer | Add-KrHtmlTemplateRoute -Path "/myroute" -HtmlTemplatePath "C:\Templates\mytemplate.html" -PassTh |
| | | 36 | | Adds a new HTML template route to the current Kestrun server and returns the route object |
| | | 37 | | .NOTES |
| | | 38 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | | 39 | | #> |
| | | 40 | | function Add-KrHtmlTemplateRoute { |
| | | 41 | | [KestrunRuntimeApi('Definition')] |
| | | 42 | | [CmdletBinding()] |
| | | 43 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 44 | | param( |
| | | 45 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 46 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 47 | | |
| | | 48 | | [Parameter(Mandatory = $true)] |
| | | 49 | | [alias('Path')] |
| | | 50 | | [string]$Pattern, |
| | | 51 | | |
| | | 52 | | [Parameter(Mandatory = $true)] |
| | | 53 | | [string]$HtmlTemplatePath, |
| | | 54 | | |
| | | 55 | | [Parameter()] |
| | | 56 | | [string[]]$AuthorizationSchema = $null, |
| | | 57 | | |
| | | 58 | | [Parameter()] |
| | | 59 | | [string[]]$AuthorizationPolicy = $null, |
| | | 60 | | |
| | | 61 | | [Parameter()] |
| | | 62 | | [switch]$PassThru |
| | | 63 | | ) |
| | | 64 | | begin { |
| | | 65 | | # Ensure the server instance is resolved |
| | 0 | 66 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 67 | | } |
| | | 68 | | process { |
| | | 69 | | |
| | 0 | 70 | | $options = [Kestrun.Hosting.Options.MapRouteOptions]::new() |
| | 0 | 71 | | $options.Pattern = $Pattern |
| | 0 | 72 | | if ($null -ne $AuthorizationSchema) { |
| | 0 | 73 | | $Options.RequireSchemes = $AuthorizationSchema |
| | | 74 | | } |
| | 0 | 75 | | if ($null -ne $AuthorizationPolicy) { |
| | 0 | 76 | | $Options.RequirePolicies = $AuthorizationPolicy |
| | | 77 | | } |
| | 0 | 78 | | if ([string]::IsNullOrWhiteSpace($HtmlTemplatePath)) { |
| | 0 | 79 | | throw 'HtmlTemplatePath cannot be null or empty.' |
| | | 80 | | } |
| | | 81 | | # Resolve the file path relative to the Kestrun root if necessary |
| | 0 | 82 | | $resolvedPath = Resolve-KrPath -Path $HtmlTemplatePath -KestrunRoot -Test |
| | 0 | 83 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| | | 84 | | # Call the C# extension method to add the HTML template route |
| | 0 | 85 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddHtmlTemplateRoute($Server, $options, $resolvedPath) | Out-Null |
| | 0 | 86 | | if ($PassThru) { |
| | 0 | 87 | | return $Server |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |