diff --git a/pkg/fileserver/server.go b/pkg/fileserver/server.go index d2dc5e9..8619573 100644 --- a/pkg/fileserver/server.go +++ b/pkg/fileserver/server.go @@ -4,6 +4,8 @@ import ( "errors" "log" "net/http" + + "github.com/heathcliff26/simple-fileserver/pkg/filesystem" ) type SSLConfig struct { @@ -18,7 +20,7 @@ type Fileserver struct { } func NewFileserver(webroot string, index bool) *Fileserver { - fs := CreateFilesystem(webroot, index) + fs := filesystem.CreateFilesystem(webroot, index) server := http.FileServer(fs) return &Fileserver{ server: server, diff --git a/pkg/fileserver/server_test.go b/pkg/fileserver/server_test.go index b1c1e03..bee96a6 100644 --- a/pkg/fileserver/server_test.go +++ b/pkg/fileserver/server_test.go @@ -12,7 +12,7 @@ import ( ) func TestLoggingWrapper(t *testing.T) { - fs := NewFileserver("./testdata", true) + fs := NewFileserver("../filesystem/testdata", true) req := httptest.NewRequest(http.MethodGet, "/test.html", nil) rr := httptest.NewRecorder() diff --git a/pkg/fileserver/filesystem.go b/pkg/filesystem/filesystem.go similarity index 69% rename from pkg/fileserver/filesystem.go rename to pkg/filesystem/filesystem.go index 0060756..f3418ce 100644 --- a/pkg/fileserver/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -1,4 +1,4 @@ -package fileserver +package filesystem import ( "net/http" @@ -36,26 +36,17 @@ func (ifs IndexlessFilesystem) Open(path string) (http.File, error) { return f, nil } -type IndexedFilesystem struct { - fs http.FileSystem -} - -// Simple wrapper around http.Dir.Open to obtain debug information -func (ifs IndexedFilesystem) Open(path string) (http.File, error) { - f, err := ifs.fs.Open(path) - if err != nil { - return nil, err - } - - return f, nil -} - // Return a filesystem with or without index enabled func CreateFilesystem(path string, index bool) http.FileSystem { fs := http.Dir(path) if index { - return IndexedFilesystem{fs} + return fs } else { - return IndexlessFilesystem{fs} + return NewIndexlessFilesystem(fs) } } + +// Create an indexless Filesystem from an existing filesystem +func NewIndexlessFilesystem(fs http.FileSystem) IndexlessFilesystem { + return IndexlessFilesystem{fs} +} diff --git a/pkg/fileserver/filesystem_test.go b/pkg/filesystem/filesystem_test.go similarity index 91% rename from pkg/fileserver/filesystem_test.go rename to pkg/filesystem/filesystem_test.go index a9290cb..4786f6c 100644 --- a/pkg/fileserver/filesystem_test.go +++ b/pkg/filesystem/filesystem_test.go @@ -1,4 +1,4 @@ -package fileserver +package filesystem import ( "net/http" @@ -21,13 +21,13 @@ func TestCreateFilesystem(t *testing.T) { Name: "withIndex", Path: "foo", Index: true, - ExpectedType: "fileserver.IndexedFilesystem", + ExpectedType: "http.Dir", }, { Name: "withoutIndex", Path: "foo", Index: false, - ExpectedType: "fileserver.IndexlessFilesystem", + ExpectedType: "filesystem.IndexlessFilesystem", }, } @@ -79,7 +79,7 @@ func TestIndexlessFilesystem(t *testing.T) { } func TestIndexedFilesystem(t *testing.T) { - fs := IndexedFilesystem{fs: http.Dir("./testdata")} + fs := http.Dir("./testdata") assert := assert.New(t) diff --git a/pkg/fileserver/testdata/test.html b/pkg/filesystem/testdata/test.html similarity index 100% rename from pkg/fileserver/testdata/test.html rename to pkg/filesystem/testdata/test.html diff --git a/pkg/fileserver/testdata/testdir/index.html b/pkg/filesystem/testdata/testdir/index.html similarity index 100% rename from pkg/fileserver/testdata/testdir/index.html rename to pkg/filesystem/testdata/testdir/index.html