| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes an HTML response to the HTTP response body. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Serializes the provided HTML template with variables and writes it to the HTTP response. |
| | | 6 | | .PARAMETER FilePath |
| | | 7 | | The path to the HTML file to read and write to the response. This can be a relative or absolute path. |
| | | 8 | | .PARAMETER Template |
| | | 9 | | The HTML template string to write to the response. If provided, this will override the FilePath parameter. |
| | | 10 | | .PARAMETER StatusCode |
| | | 11 | | The HTTP status code to set for the response. Defaults to 200 (OK). |
| | | 12 | | .PARAMETER Variables |
| | | 13 | | A hashtable of variables to use for template placeholders. These will be merged into the HTML template. |
| | | 14 | | .EXAMPLE |
| | | 15 | | Write-KrHtmlResponse -FilePath "C:\path\to\template.html" -StatusCode 200 -Variables @{ Title = "My Page"; Conte |
| | | 16 | | Reads the HTML file at "C:\path\to\template.html", merges in the variables, and writes the resulting HTML to the |
| | | 17 | | .NOTES |
| | | 18 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 19 | | #> |
| | | 20 | | function Write-KrHtmlResponse { |
| | | 21 | | [KestrunRuntimeApi('Route')] |
| | | 22 | | [CmdletBinding(defaultParameterSetName = 'FilePath')] |
| | | 23 | | [KestrunRuntimeApi('Route')] |
| | | 24 | | [CmdletBinding()] |
| | | 25 | | param( |
| | | 26 | | [Parameter(Mandatory = $true, ParameterSetName = 'FilePath')] |
| | | 27 | | [string]$FilePath, |
| | | 28 | | [Parameter(Mandatory = $true, ParameterSetName = 'Template')] |
| | | 29 | | [string]$Template, |
| | | 30 | | [Parameter()] |
| | | 31 | | [int]$StatusCode = 200, |
| | | 32 | | [Parameter()] |
| | | 33 | | [hashtable]$Variables |
| | | 34 | | ) |
| | | 35 | | # Only works inside a route script block where $Context is available |
| | 0 | 36 | | if ($null -ne $Context.Response) { |
| | | 37 | | try { |
| | 0 | 38 | | if ($null -eq $Variables) { $Variables = @{} } |
| | | 39 | | # Wrap the hashtable in a read-only dictionary for safety |
| | 0 | 40 | | $readOnlyDictionary = [Kestrun.Utilities.ReadOnlyDictionaryAdapter]::new($Variables) |
| | | 41 | | |
| | 0 | 42 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 43 | | 'FilePath' { |
| | | 44 | | # Resolve the file path relative to the Kestrun root if necessary |
| | 0 | 45 | | $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test |
| | 0 | 46 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| | | 47 | | # Call the C# method on the $Context.Response object |
| | 0 | 48 | | $Context.Response.WriteHtmlResponseFromFile($resolvedPath, $readOnlyDictionary, $StatusCode) |
| | | 49 | | } |
| | | 50 | | 'Template' { |
| | | 51 | | # Call the C# method on the $Context.Response object |
| | 0 | 52 | | $Context.Response.WriteHtmlResponse($Template, $readOnlyDictionary, $StatusCode) |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | } catch { |
| | | 56 | | # Handle any errors that occur during the file response writing |
| | 0 | 57 | | Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_ |
| | | 58 | | } |
| | | 59 | | } else { |
| | 0 | 60 | | Write-KrOutsideRouteWarning |
| | | 61 | | } |
| | | 62 | | } |