Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

static: only strip if the file path is not a single slash #171

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions static.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ func Static(opts ...StaticOptions) Handler {
}
}

// The go embed file system returns an error when the path ends with a slash.
file = strings.TrimRight(file, "/")
// The go embed file system returns an error when the path ends with a slash or the path is empty.
if file == "/" {
file = "."
} else {
file = strings.TrimRight(file, "/")
}

f, err := opt.FileSystem.Open(file)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/assert"
)

//go:embed internal
//go:embed internal README.md
var embedFS embed.FS

func TestStatic(t *testing.T) {
Expand Down Expand Up @@ -142,6 +142,11 @@ func TestStatic_Options(t *testing.T) {
index: "README.md",
wantStatusCode: http.StatusOK,
},
{
uri: "/embed/",
index: "README.md",
wantStatusCode: http.StatusOK,
},
}
for _, test := range tests {
t.Run(test.index, func(t *testing.T) {
Expand Down