Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit cf11182

Browse files
bateskevinTiberriver256
authored andcommitted
Fix #17: Adding default files for static routes (#141)
* Solved Issue #17. Code added to New-PolarisStaticRoute.ps1 and test New-PolarisStaticRoute.Tests.ps1 edited. * Updating file serve logic with suggested changes * Removed unit test and added e2e test * Added the docs * Removed extra whitespace and added a newline * Setting $ServeDefaultFile to $True * Modified test for new behavior
1 parent ed69a59 commit cf11182

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

Public/New-PolarisStaticRoute.ps1

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77
.SYNOPSIS
88
Creates web routes to recursively serve folder contents
99
.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.
1113
.PARAMETER RoutePath
1214
Root route that the folder path will be served to.
1315
Defaults to "/".
1416
.PARAMETER FolderPath
1517
Full path and name of the folder to serve.
1618
.PARAMETER EnableDirectoryBrowser
1719
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
1825
.PARAMETER Force
1926
Use -Force to overwrite existing web route(s) for the same paths.
2027
.PARAMETER Polaris
@@ -47,6 +54,11 @@ function New-PolarisStaticRoute {
4754
[switch]
4855
$Force,
4956

57+
[string[]]
58+
$StandardHTMLFiles = @("index.html", "index.htm", "default.html", "default.htm"),
59+
60+
[bool]
61+
$ServeDefaultFile = $True,
5062

5163
$Polaris = $Script:Polaris
5264
)
@@ -74,9 +86,20 @@ function New-PolarisStaticRoute {
7486
$Content = ""
7587

7688
$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+
}
7798
Write-Debug "Parsed local path: $LocalPath"
7899
try {
100+
79101
$RequestedItem = Get-Item -LiteralPath "$NewDrive`:$LocalPath" -Force -ErrorAction Stop
102+
80103
Write-Debug "Requested Item: $RequestedItem"
81104

82105
if ($RequestedItem.PSIsContainer) {
@@ -117,14 +140,25 @@ function New-PolarisStaticRoute {
117140
}
118141
}
119142

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+
120156
# Inserting variables into scriptblock as hardcoded
121157
$Scriptblock = [scriptblock]::Create(
122-
"`$RoutePath = '$($RoutePath.TrimStart("/"))'`r`n" +
123-
"`$EnableDirectoryBrowser = `$$EnableDirectoryBrowser`r`n" +
124-
"`$NewDrive = '$NewDrive'`r`n" +
158+
$Parameters +
125159
$Scriptblock.ToString())
126160

127-
$PolarisPath = "$RoutePath/:FilePath?" -replace "//", "/"
128161

162+
$PolarisPath = "$RoutePath/:FilePath?" -replace "//", "/"
129163
New-PolarisRoute -Path $PolarisPath -Method GET -Scriptblock $Scriptblock -Force:$Force -ErrorAction:$ErrorAction
130164
}

Tests/e2e/New-PolarisStaticRoute.Tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ Describe "New-PolarisStaticRoute (E2E)" {
3131
$File1Uri = "http://localhost:$Port/$RootPath/File1.txt"
3232
$File1Content = 'File1Content'
3333
$File1Content | Out-File -FilePath $File1 -Encoding ascii -NoNewline
34+
'Hello!' | Out-File -FilePath "$TestPath\default.html" -Encoding ascii -NoNewline
3435

3536

3637

3738
Start-Job -Scriptblock {
38-
3939
# Import module
4040
Import-Module $using:PSScriptRoot\..\..\Polaris.psd1
4141

4242
# Start with a clean slate
4343
Remove-PolarisRoute
4444

4545
#### Create static routes
46-
New-PolarisStaticRoute -RoutePath $using:RootPath -FolderPath $using:TestPath
46+
New-PolarisStaticRoute -RoutePath $using:RootPath -FolderPath $using:TestPath -ServeDefaultFile $True
4747
Write-Output "RootPath: $using:RootPath --- FolderPath: $using:TestPath"
4848

4949
#### Create a static route at the root
@@ -78,10 +78,17 @@ Describe "New-PolarisStaticRoute (E2E)" {
7878
It "Should serve a directory browser when enabled" {
7979

8080
# Confirm file can be downloaded
81-
$Download = Invoke-WebRequest -Uri "http://localhost:$Port/" -UseBasicParsing
81+
$Download = Invoke-WebRequest -Uri "http://localhost:$Port/Sub1" -UseBasicParsing
8282
$Download.Content | Should BeLike "*Polaris Static File Server*"
8383
}
8484

85+
It "Should serve a default file when enabled" {
86+
87+
# Confirm file can be downloaded
88+
$Download = Invoke-WebRequest -Uri "http://localhost:$Port/$RootPath/" -UseBasicParsing
89+
$Download.Content | Should BeLike "*Hello*"
90+
}
91+
8592
AfterAll {
8693

8794
# Clean up test routes

0 commit comments

Comments
 (0)