Skip to content

Commit

Permalink
filesystem: Move filesystem into separate package
Browse files Browse the repository at this point in the history
Signed-off-by: Heathcliff <[email protected]>
  • Loading branch information
heathcliff26 committed Jan 3, 2025
1 parent 890f016 commit fe24d7e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
4 changes: 3 additions & 1 deletion pkg/fileserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"log"
"net/http"

"github.com/heathcliff26/simple-fileserver/pkg/filesystem"
)

type SSLConfig struct {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fileserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 8 additions & 17 deletions pkg/fileserver/filesystem.go → pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fileserver
package filesystem

import (
"net/http"
Expand Down Expand Up @@ -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}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fileserver
package filesystem

import (
"net/http"
Expand All @@ -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",
},
}

Expand Down Expand Up @@ -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)

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit fe24d7e

Please sign in to comment.