|
7 | 7 | .SYNOPSIS
|
8 | 8 | Creates web routes to recursively serve folder contents
|
9 | 9 | .DESCRIPTION
|
10 |
| - Creates web routes to recursively serve folder contents. Perfect for static websites. |
| 10 | + Creates web routes to recursively serve folder contents. Perfect for static websites. |
| 11 | + Also if a default file (for example index.html) is detected, A route pointing to the |
| 12 | + value of the parameter "Routepath" will be created. |
11 | 13 | .PARAMETER RoutePath
|
12 | 14 | Root route that the folder path will be served to.
|
13 | 15 | Defaults to "/".
|
14 | 16 | .PARAMETER FolderPath
|
15 | 17 | Full path and name of the folder to serve.
|
16 | 18 | .PARAMETER EnableDirectoryBrowser
|
17 | 19 | Enables the directory browser when the user requests a folder
|
| 20 | +.PARAMETER ServeDefaultFile |
| 21 | + Polaris will look for a default file matching one of the file names specified in the |
| 22 | + StandardHTMLFiles parameter and, if found, will serve that file when no specific file is requested |
| 23 | +.PARAMETER StandardHTMLFiles |
| 24 | + List of file names that Polaris will look for when ServeDefaultFile is $True |
18 | 25 | .PARAMETER Force
|
19 | 26 | Use -Force to overwrite existing web route(s) for the same paths.
|
20 | 27 | .PARAMETER Polaris
|
@@ -47,6 +54,11 @@ function New-PolarisStaticRoute {
|
47 | 54 | [switch]
|
48 | 55 | $Force,
|
49 | 56 |
|
| 57 | + [string[]] |
| 58 | + $StandardHTMLFiles = @("index.html", "index.htm", "default.html", "default.htm"), |
| 59 | + |
| 60 | + [bool] |
| 61 | + $ServeDefaultFile = $True, |
50 | 62 |
|
51 | 63 | $Polaris = $Script:Polaris
|
52 | 64 | )
|
@@ -74,9 +86,20 @@ function New-PolarisStaticRoute {
|
74 | 86 | $Content = ""
|
75 | 87 |
|
76 | 88 | $LocalPath = $Request.Parameters.FilePath
|
| 89 | + if (-not $LocalPath -and $ServeDefaultFile) { |
| 90 | + foreach ($FileName in $StandardHTMLFiles) { |
| 91 | + $FilePath = Join-Path "$($NewDrive):" -ChildPath "$FileName" |
| 92 | + if (Test-Path -Path $FilePath) { |
| 93 | + $LocalPath = $FileName |
| 94 | + break |
| 95 | + } |
| 96 | + } |
| 97 | + } |
77 | 98 | Write-Debug "Parsed local path: $LocalPath"
|
78 | 99 | try {
|
| 100 | + |
79 | 101 | $RequestedItem = Get-Item -LiteralPath "$NewDrive`:$LocalPath" -Force -ErrorAction Stop
|
| 102 | + |
80 | 103 | Write-Debug "Requested Item: $RequestedItem"
|
81 | 104 |
|
82 | 105 | if ($RequestedItem.PSIsContainer) {
|
@@ -117,14 +140,25 @@ function New-PolarisStaticRoute {
|
117 | 140 | }
|
118 | 141 | }
|
119 | 142 |
|
| 143 | + $Parameters = "`$RoutePath = '$($RoutePath.TrimStart("/"))'`r`n" + |
| 144 | + "`$NewDrive = '$NewDrive'`r`n" |
| 145 | + |
| 146 | + if ($EnableDirectoryBrowser) { |
| 147 | + $Parameters += "`$EnableDirectoryBrowser = `$$EnableDirectoryBrowser`r`n" |
| 148 | + } |
| 149 | + if ($ServeDefaultFile) { |
| 150 | + $Parameters += "`$ServeDefaultFile = `$$ServeDefaultFile`r`n" |
| 151 | + $Parameters += "`$StandardHTMLFiles = @('$( $StandardHTMLFiles -join "','" )')`r`n" |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + |
120 | 156 | # Inserting variables into scriptblock as hardcoded
|
121 | 157 | $Scriptblock = [scriptblock]::Create(
|
122 |
| - "`$RoutePath = '$($RoutePath.TrimStart("/"))'`r`n" + |
123 |
| - "`$EnableDirectoryBrowser = `$$EnableDirectoryBrowser`r`n" + |
124 |
| - "`$NewDrive = '$NewDrive'`r`n" + |
| 158 | + $Parameters + |
125 | 159 | $Scriptblock.ToString())
|
126 | 160 |
|
127 |
| - $PolarisPath = "$RoutePath/:FilePath?" -replace "//", "/" |
128 | 161 |
|
| 162 | + $PolarisPath = "$RoutePath/:FilePath?" -replace "//", "/" |
129 | 163 | New-PolarisRoute -Path $PolarisPath -Method GET -Scriptblock $Scriptblock -Force:$Force -ErrorAction:$ErrorAction
|
130 | 164 | }
|
0 commit comments